Python 将json转换为x-www-form-urlencoded
如何将json转换为用Python编码的x-www-form-url 我使用下面的代码进行转换,但它与邮递员编码的值不同,因此我得到了一个错误Python 将json转换为x-www-form-urlencoded,python,urlencode,Python,Urlencode,如何将json转换为用Python编码的x-www-form-url 我使用下面的代码进行转换,但它与邮递员编码的值不同,因此我得到了一个错误 import json from urllib.parse import urlencode j = json.dump('json code') encoded = urlencode(json.loads(j),encode='utf-8') 我的POST请求中出现以下错误 "error": { "status": 500, "m
import json
from urllib.parse import urlencode
j = json.dump('json code')
encoded = urlencode(json.loads(j),encode='utf-8')
我的POST请求中出现以下错误
"error": {
"status": 500,
"message": "Unexpected token '",
"message_shortcode": "unexpected token '",
"datetime": "20180102131407",
"url": "http://127.0.0.1:3001/api/searches/0SH69023763",
"qs": "type=form&result_options=%7B%22fieldset%22%3A%5B%22count%22%5D%7D",
"type": "InternalServerError",
"group": "server"
}
很明显,服务器API期望的数据没有以期望的格式传递,这破坏了服务器端代码的执行请尝试检查API的确切要求以及标题 如果它希望内容类型头是“x-www-form-urlencoded”,那么请确保在向API URL发出请求时也将其传递
使用requests模块,即使不使用urllib的urlencode,也可以更轻松地实现代码。因此,您最好也看看这里:什么python编码器的工作原理与postman的“x-www-form-encoded”相同?您是否尝试了请求模块来执行API url?您只需要在requests.get()或requests.post()方法的数据参数中直接传递字典,而根本不需要urlencoding,默认情况下,如果您在requests.get()或requests.post()方法的数据参数中传递字典,则“x-www-form-urlencoded”格式将是默认用法。例如,对于您的数据:api_resp=requests.post(api_url,data=json.loads(j),timeout=5)。请参阅此处的更多信息:您传递的查询字符串包含嵌入的json,该字符串用单引号引起来:
{'type':['form'],'result_options':['{“fieldset”:[“count”]}]}
-也许API在这里不需要json?