Python HTTP post Json 400错误
我正试图将数据从微控制器发送到服务器。我需要从我的控制器发送原始http数据,这是我在下面发送的数据:Python HTTP post Json 400错误,python,json,http-post,Python,Json,Http Post,我正试图将数据从微控制器发送到服务器。我需要从我的控制器发送原始http数据,这是我在下面发送的数据: POST /postpage HTTP/1.1 Host: https://example.com Accept: */* Content-Length: 18 Content-Type: application/json {"cage":"abcdefg"} 我的服务器需要JSON编码,而不是表单编码请求 对于上面发送的请求,我从服务器得到一个400错误,HTTP/1.1400 Bad
POST /postpage HTTP/1.1
Host: https://example.com
Accept: */*
Content-Length: 18
Content-Type: application/json
{"cage":"abcdefg"}
我的服务器需要JSON编码,而不是表单编码请求
对于上面发送的请求,我从服务器得到一个400错误,HTTP/1.1400 Bad request
然而,当我试图通过笔记本电脑通过python脚本访问我的服务器时,我能够得到正确的响应
import requests
url='https://example.com'
mycode = 'abcdefg'
def enter():
value = requests.post('url/postpage',
params={'cage': mycode})
print vars(value)
enter()
有人能告诉我我在上面发送的原始http数据中哪里可能出错吗?http将标题之间的分隔符指定为一个换行符,并且在内容之前需要一个双换行符:
POST /postpage HTTP/1.1
Host: https://example.com
Accept: */*
Content-Length: 18
Content-Type: application/json
{"cage":"abcdefg"}
如果您认为所有请求都不正确,请尝试查看Python发送的内容:
response = ...
request = response.request # request is a PreparedRequest.
headers = request.headers
url = request.url
有关更多信息,请阅读
要传递参数,请使用以下Python:
REQUEST = 'POST /postpage%s HTTP/1.1\r\nHost: example.com\r\nContent-Length: 0\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.4.3 CPython/2.7.9 Linux/4.4.11-v7+\r\n\r\n';
query = ''
for k, v in params.items():
query += '&' + k + '=' + v # URL-encode here if you want.
if len(query): query = '?' + query[1:]
return REQUEST % query
我使用了与您发布的相同的语法。只是在我的帖子里没有正确的格式。在帖子中更正了它。@JF这也不起作用。是否有可能看到python脚本实际发送的http原始数据,以便我可以在我的微控制器上复制这些原始数据?@J F我实际上成功地获得了python正在发送的数据(从服务器获得了成功回复)。它是POST/postpage?cage=abcdefg HTTP/1.1\r\nHost:\r\n内容长度:0\r\n连接:保持活动\r\n接受编码:gzip,deflate\r\n接受:*/*\r\n用户代理:python请求/2.4.3 CPython/2.7.9 Linux/4.4.11-v7+\r\n\r\n。cage:abcdefg被附加到Post URL,而不是作为数据发送。如何修改原始帖子中发布的http原始数据流,以便在帖子中附加cage:abcdefg?