【发布时间】:2018-06-11 23:07:28
【问题描述】:
如何在 Python 中将 json 转换为 x-www-form-urlencoded?
我正在使用下面的代码进行转换,但它与 Postman 编码的值不同,因此出现错误。
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"
}
【问题讨论】:
-
您传递的查询字符串包含嵌入的 json,用单引号括起来:
{'type': ['form'], 'result_options': ['{"fieldset":["count"]}']}- 也许 API 不希望这里有 json?