什么是json.decoder.JSONDecodeError:期望值:第1行第1列(char 0)

时间:2018-02-21 23:06:38

标签: python json api curl

我正在请求面对比较API。你发送2张图片的地方,如果他们是同一个人则返回这里是链接: Face++ API

在文档中,他们提到您可以这样请求:

curl -X POST "https://api-us.faceplusplus.com/facepp/v3/compare" \
-F "api_key=<api_key>" \
-F "api_secret=<api_secret>" \
-F "face_token1=c2fc0ad7c8da3af5a34b9c70ff764da0" \
-F "face_token2=ad248a809408b6320485ab4de13fe6a9"

这是我的代码:

result = json.loads(os.popen('curl -X POST "https://api-us.faceplusplus.com/facepp/v3/compare/api_key=myKey/api_secret=mySecret/https://us-mc-prod-asset.oss-us-west-1.aliyuncs.com/mc-official/scripts/demoScript/images/demo-pic33.jpg/https://us-mc-prod-asset.oss-us-west-1.aliyuncs.com/mc-official/scripts/demoScript/images/demo-pic39.jpg"').read())

但是当我运行它时,我得到了这个:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
Traceback (most recent call last):
  File "/Users/max/Desktop/Programming/Face/venv/face.py", line 15, in <module>
    result = json.loads(os.popen('curl -X POST "https://api-us.faceplusplus.com/facepp/v3/compare/api_key=myKey/api_secret=mySecret/https://us-mc-prod-asset.oss-us-west-1.aliyuncs.com/mc-official/scripts/demoScript/images/demo-pic33.jpg/https://us-mc-prod-asset.oss-us-west-1.aliyuncs.com/mc-official/scripts/demoScript/images/demo-pic39.jpg"').read())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我试图以另一种方式使用请求:

result = requests.get( "https://api-us.faceplusplus.com/facepp/v3/compare/api_key=myKey/api_secret=mySecret/https://us-mc-prod-asset.oss-us-west-1.aliyuncs.com/mc-official/scripts/demoScript/images/demo-pic33.jpg/https://us-mc-prod-asset.oss-us-west-1.aliyuncs.com/mc-official/scripts/demoScript/images/demo-pic39.jpg")

但是,我得到了这个:

<Response [404]>

1 个答案:

答案 0 :(得分:0)

curl命令格式错误,因此您收到错误Could not resolve host: api_key=myKey而不是有效的JSON返回数据。

但是,(不要脱离你的问题),我是否可以建议您使用requests(如果必须使用标准python库,则为urllib2)来发送HTTP请求。以你自己的方式调用curl会产生意想不到的结果,例如你现在正在处理的事情。

此外,requests实际上对响应对象有一个非常方便的json()方法。这将使您的代码更清晰,更容易进行故障排除。以下是文档中的示例:

http://docs.python-requests.org/en/master/user/quickstart/#json-response-content