【问题标题】:Convert PHP API call to Python将 PHP API 调用转换为 Python
【发布时间】:2019-12-16 16:50:22
【问题描述】:

我有一个现有的 API,我使用 PHP 连接并发送图像,代码如下所示...

$url = 'https://example.com/api';
$ch = \curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Subscription-Key: 345sdf4'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
            \"fileurl\":\"" . $this->_config['server_config']['server_url'] . $my_image . "\"
        }");

我正在尝试将其转换为 Python3,我知道我需要 requests 所以到目前为止...

import requests

api_url_base = 'https://example.com/api'
headers = {'Content-Type': 'application/json',
       'Subscription-Key': '345sdf4'}

但据我所知,如何添加图像?我已经知道图片的绝对路径了

【问题讨论】:

  • 您的 POST 正文将类似于 json.dumps(dict({'fileurl': myUrl}))

标签: php python python-3.x


【解决方案1】:

根据这个post,您可以执行以下操作:

import requests
import json

url = 'https://example.com/api'
body = {'name': 'something'}
headers = {'content-type': 'application/json'}

r = requests.post(url, data=json.dumps(body), headers=headers)

source

【讨论】:

    猜你喜欢
    • 2019-07-27
    • 2023-04-08
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 2012-12-03
    相关资源
    最近更新 更多