Python中的HTTP POST
我正在尝试将HTTP POST转换为Python,但不确定如何执行此操作 我有HTTP请求:Python中的HTTP POST,python,http,post,Python,Http,Post,我正在尝试将HTTP POST转换为Python,但不确定如何执行此操作 我有HTTP请求: POST vision/v1/ocr?language=unk&detectOrientation =true Content-Type: application/json Host: api.projectoxford.ai Content-Length: 95 Ocp-Apim-Subscription-Key: •••••••••••••••••••••••••••••••• { "Url
POST vision/v1/ocr?language=unk&detectOrientation =true
Content-Type: application/json
Host: api.projectoxford.ai
Content-Length: 95
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••
{ "Url": "exampleurl.com"}
以及URL请求,并需要有关我是否正确执行此操作的帮助
import urllib.parse
import urllib.request
url = "urlrequest.com"
values = {"Url":
https://exampleurl.com}
data = url.lib.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(url, data)
with urllib.request.urlopen(req) as response:
the_page = response.read()
我得到一个htttperor
:
HTTP Error 400: Bad Request.
您需要发送带有附加标题项的JSON:
url = "http://api.projectoxford.ai/vision/v1/ocr?language=unk&detectOrientation=true"
data = json.dumps({'Url': 'exampleurl.com'}).encode('utf-8')
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '••••••••••••••••••••••••••••••••',
}
req = urllib.request.Request(url, data, headers)
with urllib.request.urlopen(req) as response:
the_page = response.read()
req
未定义。显示正确的代码。您的代码有语法错误,编码后也没有实际使用数据。请至少向我们提供不存在语法错误的代码,以便我们确定是否存在不使用数据的问题。我很抱歉。我漏了一行代码。现在就可以显示我的代码了。json.dumps()的作用是什么?我得到“TypeError:POST数据应该是字节或字节的iterable。它不能是str类型。”