Python3:没有请求库的 JSON POST 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25491541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:18:20  来源:igfitidea点击:

Python3: JSON POST Request WITHOUT requests library

pythonjsonurllib

提问by moritzg

I want to send JSON encoded data to a server using only native Python libraries. I love requests but I simply can't use it because I can't use it on the machine which runs the script. I need to do it without.

我想仅使用本机 Python 库将 JSON 编码的数据发送到服务器。我喜欢请求,但我根本无法使用它,因为我无法在运行脚本的机器上使用它。我需要在没有的情况下做到这一点。

newConditions = {"con1":40, "con2":20, "con3":99, "con4":40, "password":"1234"} 
params = urllib.parse.urlencode(newConditions)
params = params.encode('utf-8')

req = urllib.request.Request(conditionsSetURL, data=params)
urllib.request.urlopen(req)        

My server is a local WAMP server. I always get an

我的服务器是本地 WAMP 服务器。我总是得到一个

urllib.error.HTTPError: HTTP Error 500: Internal Server Error

urllib.error.HTTPError:HTTP 错误 500:内部服务器错误

I am 100% surethat this is NOTa server issue, because the same data, with the same url, on the same machine, with the same server works with the requests library and Postman.

100% 确定不是服务器问题,因为相同的数据,具有相同的 url,在同一台机器上,具有相同的服务器与请求库和邮递员一起工作。

采纳答案by Martijn Pieters

You are not posting JSON, you are posting a application/x-www-form-urlencodedrequest.

您不是在发布 JSON,而是在发布application/x-www-form-urlencoded请求。

Encode to JSON and set the right headers:

编码为 JSON 并设置正确的标头:

import json

newConditions = {"con1":40, "con2":20, "con3":99, "con4":40, "password":"1234"} 
params = json.dumps(newConditions).encode('utf8')
req = urllib.request.Request(conditionsSetURL, data=params,
                             headers={'content-type': 'application/json'})
response = urllib.request.urlopen(req)

Demo:

演示:

>>> import json
>>> import urllib.request
>>> conditionsSetURL = 'http://httpbin.org/post'
>>> newConditions = {"con1":40, "con2":20, "con3":99, "con4":40, "password":"1234"} 
>>> params = json.dumps(newConditions).encode('utf8')
>>> req = urllib.request.Request(conditionsSetURL, data=params,
...                              headers={'content-type': 'application/json'})
>>> response = urllib.request.urlopen(req)
>>> print(response.read().decode('utf8'))
{
  "args": {}, 
  "data": "{\"con4\": 40, \"con2\": 20, \"con1\": 40, \"password\": \"1234\", \"con3\": 99}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept-Encoding": "identity", 
    "Connection": "close", 
    "Content-Length": "68", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "Python-urllib/3.4", 
    "X-Request-Id": "411fbb7c-1aa0-457e-95f9-1af15b77c2d8"
  }, 
  "json": {
    "con1": 40, 
    "con2": 20, 
    "con3": 99, 
    "con4": 40, 
    "password": "1234"
  }, 
  "origin": "84.92.98.170", 
  "url": "http://httpbin.org/post"
}