将PHP API调用转换为Python
我有一个现有的API,我连接到使用PHP和发送图像,代码看起来像这样将PHP API调用转换为Python,php,python,python-3.x,Php,Python,Python 3.x,我有一个现有的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,
$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,我知道我需要请求
,所以到目前为止,我已经有了这个
import requests
api_url_base = 'https://example.com/api'
headers = {'Content-Type': 'application/json',
'Subscription-Key': '345sdf4'}
但就我所知,如何添加图像?我已经知道图像的绝对路径根据此,您可以执行以下操作:
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)
根据此,您可以执行以下操作:
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)
您的
POST
正文类似于json.dumps(dict({'fileurl':myUrl}))
您的POST
正文类似于json.dumps(dict({'fileurl':myUrl}))