最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python接口返回400错误:{‘detail’: ‘JSON parse error – Expecting value: line 1 column 1 (char 0)’}

Python crifan 9785浏览 0评论

折腾:

【已解决】把文本格式的剧本内容用Python批量导入后台系统

期间,用代码:

<code>gHeaders = {
  'Content-Type': 'application/json; charset=utf-8',
  "Accept": 'application/json',
  "Authorization": "",
}

logging.info("curScriptDict=%s", curScriptDict)
saveScriptResp = requests.post(CreateScriptUrl, headers=gHeaders, data=curScriptDict)
logging.info("saveScriptResp=%s", saveScriptResp)
respJson = saveScriptResp.json()
logging.info("respJson=%s", respJson)
</code>

调用后台接口,结果返回400:

<code>20180717 11:46:50 BatchImportScript.py:201  INFO    curScriptDict={'operate_mark': 'save', 'place': 'School canteen', 'title': 'Have lunch', 'age_start': 3, 'age_end': 4, 'topic': 'Food', 'second_level_topic': '', 'dialogs': [{'type': '0', 'speaker': 'J', 'content': 'What did you have for lunch?'}, {'type': '0', 'speaker': 'L', 'content': 'I ate rice, fish and bread.'}, {'type': '0', 'speaker': 'J', 'content': 'Do you like rice?'}, {'type': '0', 'speaker': 'L', 'content': 'Yes, I do.'}, {'type': '0', 'speaker': 'J', 'content': 'Do you like fish?'}, {'type': '0', 'speaker': 'L', 'content': 'Yes, I do.'}, {'type': '0', 'speaker': 'J', 'content': 'Do you like bread?'},{'type': '0', 'speaker': 'L', 'content': 'No, I don’t.'}, {'type': '0', 'speaker': 'J', 'content': 'What did you drink?'}, {'type': '0', 'speaker': 'L', 'content': 'I drank milk.'}, {'type': '0', 'speaker': 'J', 'content': 'Do you like milk?'}, {'type': '0', 'speaker':'L', 'content': 'Yes, I do.'}]}
20180717 11:46:57 BatchImportScript.py:203  INFO    saveScriptResp=&lt;Response [400]&gt;
20180717 11:47:10 BatchImportScript.py:205  INFO    respJson={'detail': 'JSON parse error - Expecting value: line 1 column 1 (char 0)'}
</code>

python {‘detail’: ‘JSON parse error – Expecting value: line 1 column 1 (char 0)’}

python 400 {‘detail’: ‘JSON parse error – Expecting value: line 1 column 1 (char 0)’}

快速上手 — Requests 2.18.1 文档

是用data传递参数的:

<code>&gt;&gt;&gt; r = requests.post('http://httpbin.org/post', data = {'key':'value'})
</code>

python – JSONDecodeError: Expecting value: line 1 column 1 (char 0) – Stack Overflow

jquery – JSON parse error on POST request – Stack Overflow

注意到:

另外的web端:

src/utils/request.js

调用时是传递的stringfy后的字符串:

<code>      newOptions.headers = {
        Accept: 'application/json',
        'Content-Type': 'application/json; charset=utf-8',
        ...newOptions.headers,
      };
      newOptions.body = JSON.stringify(newOptions.body);
    } 
</code>

以及:

https://stackoverflow.com/questions/45393958/json-parse-error-on-post-request

<code>data: JSON.stringify("{"email": "[email protected]"}")
contentType: "application/json"
</code>

所以此处也去把dict的json转换为字符串试试

另外更加注意到:

JSON parse error

指的是:JSON解析(从字符串?)时出的错

-》所以应该就是传递进去的,需要是字符串(而不是Json的Dict字典)

然后去试试:

<code>scritpJsonStr = json.dumps(curScriptDict)
logging.info("scritpJsonStr=%s", scritpJsonStr)
saveScriptResp = requests.post(CreateScriptUrl, headers=gHeaders, data=scritpJsonStr)
</code>

然后就可以了:

另外,待会去试试:

http://docs.python-requests.org/zh_CN/latest/api.html

http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

中提到的:

直接在post时传递json:

““此处除了可以自行对 dict 进行编码,你还可以使用 json 参数直接传递,然后它就会被自动编码。这是 2.4.2 版的新加功能:

>>> url = ‘https://api.github.com/some/endpoint’

>>> payload = {‘some’: ‘data’}

>>> r = requests.post(url, json=payload)”

用:

<code>saveScriptResp = requests.post(CreateScriptUrl, headers=gHeaders, json=curScriptDict)
</code>

结果也是一样正常的。

【总结】

此处是Python调用后台API时,虽然传递了:

<code>gHeaders = {
  'Content-Type': 'application/json; charset=utf-8',
  "Accept": 'application/json',
}
</code>

的header,但是传递POST的body的data时,传递的是dict的json对象,导致:

Python内部调用json去尝试把(以为的)json字符串去decode,结果报错:

{‘detail’: ‘JSON parse error – Expecting value: line 1 column 1 (char 0)’}

解决办法是:

确保传递POST的body的data是:(json格式的)字符串,而不是(dict的)json对象

即:

<code>import requests
import json

scritpJsonStr = json.dumps(curScriptDict)
saveScriptResp = requests.post(CreateScriptUrl, headers=gHeaders, data=scritpJsonStr)
</code>

或:

<code>import requests

</code>

saveScriptResp = requests.post(CreateScriptUrl, headers=gHeaders, json=curScriptDict)

即可正常传入。

转载请注明:在路上 » 【已解决】Python接口返回400错误:{‘detail’: ‘JSON parse error – Expecting value: line 1 column 1 (char 0)’}

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.167 seconds, using 22.15MB memory