您不发布JSON,而是发布
application/x-www-form-urlenpred请求。
编码为JSON并设置正确的标题:
import jsonnewConditions = {"con1":40, "con2":20, "con3":99, "con4":40, "password":"1234"} params = json.dumps(newConditions).enpre('utf8')req = urllib.request.Request(conditionsSetURL, data=params, headers={'content-type': 'application/json'})response = urllib.request.urlopen(req)演示:
>>> 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).enpre('utf8')>>> req = urllib.request.Request(conditionsSetURL, data=params,... headers={'content-type': 'application/json'})>>> response = urllib.request.urlopen(req)>>> print(response.read().depre('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"}


