最新消息:20181230 VPS服务器已从Linode换到腾讯云香港,主题仍用朋友推荐的大前端D8

【已解决】curl去POST传JSON失败:Failed to decode JSON object Expecting property name enclosed in double quotes

JSON crifan 1423浏览 0评论

折腾:

【已解决】Flask的rest的api返回出错:Invalid HTTP Version HTTP/1.1

期间,通过curl去测试接口,POST时传入了json参数,且也设置了header:

-H “Content-Type:application/json”

但是还是失败:

➜  ~ curl http://115.29.173.126:21084/runningfast/api/v1.0/open/register -X POST -v -H “Content-Type:application/json” -d “{“email”:”xxxx@qq.com”,”facebookUserId”:”15xxx4″,”firstName”:”l”,”lastName”:”bj”,”password”:”111111″,”phone”:”1xxx”,”smsCode”:”157341″}”
Note: Unnecessary use of -X or –request, POST is already inferred.
*   Trying 115.29.173.126…
* Connected to 115.29.173.126 (115.29.173.126) port 21084 (#0)
> POST /runningfast/api/v1.0/open/register HTTP/1.1
> Host: 115.29.173.126:21084
> User-Agent: curl/7.49.1
> Accept: */*
> Content-Type:application/json
> Content-Length: 129
>
* upload completely sent off: 129 out of 129 bytes
< HTTP/1.1 400 BAD REQUEST
< Server: gunicorn/19.6.0
< Date: Wed, 12 Oct 2016 06:13:06 GMT
< Connection: close
< Content-Type: application/json
< Content-Length: 127
<
{
    “message”: “Failed to decode JSON object: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)”
}
* Closing connection 0

用curl去post时传入json

出错:

Failed to decode JSON object: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

curl post json参数

curl post传递json数据 – youxin – 博客园

GitHub – jkbrzt/httpie: Command line HTTP client, a user-friendly curl alternative with an intuitive UI, JSON support, syntax highlighting, wget-like downloads, extensions, etc. Follow https://twitter.com/clihttp for tips and updates.

都打算用两个双引号的:

curl http://115.29.173.126:21084/runningfast/api/v1.0/open/register -X POST -v -H “Content-Type:application/json” -d “{“”phone””:””18790639671″”}”

curl post json

How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST? – Stack Overflow

好像是:

最外层用单引号:

就可以了:

➜  ~ curl http://115.29.173.126:21084/runningfast/api/v1.0/open/register -X POST -v -H “Content-Type:application/json” -d ‘{“email”:”xxx@qq.com”,”facebookUserId”:”15xxx4″,”firstName”:”l”,”lastName”:”bj”,”password”:”111111″,”phone”:”18xxx”,”smsCode”:”157341″}’
Note: Unnecessary use of -X or –request, POST is already inferred.
*   Trying 115.29.173.126…
* Connected to 115.29.173.126 (115.29.173.126) port 21084 (#0)
> POST /runningfast/api/v1.0/open/register HTTP/1.1
> Host: 115.29.173.126:21084
> User-Agent: curl/7.49.1
> Accept: */*
> Content-Type:application/json
> Content-Length: 157
>
* upload completely sent off: 157 out of 157 bytes
< HTTP/1.1 200 OK
< Server: gunicorn/19.6.0
< Date: Wed, 12 Oct 2016 06:26:04 GMT
< Connection: close
< Content-Type: application/json
< Content-Length: 73
<
{
    “code”: 10302,
    “message”: “sms code has expired or invalid”
}
* Closing connection 0

【总结】

用curl去POST时,传入JSON时,最外层不能用双引号,而要用单引号:

-d ‘{“password”:”111111″}’

就可以了。

当然,记得指定json:

完整的例子:

curl http://115.29.173.126:21084/runningfast/api/v1.0/open/register -X POST -v -H “Content-Type:application/json” -d ‘{“email”:”2xxx@qq.com”,”facebookUserId”:”15xxx4″,”firstName”:”l”,”lastName”:”bj”,”password”:”111111″,”phone”:”18xxxx”,”smsCode”:”157341″}’

转载请注明:在路上 » 【已解决】curl去POST传JSON失败:Failed to decode JSON object Expecting property name enclosed in double quotes

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
67 queries in 0.091 seconds, using 9.62MB memory