使用基本身份验证通过 CURL POST JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6109227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
POST JSON over CURL with basic authentication
提问by 2bard
I am using Curl from the command line to debug a small web api I am working on. The web api expects basic authentication and a JSON object as input (POST). Currently this basic authentications works fine:
我正在从命令行使用 Curl 来调试我正在处理的一个小型 web api。Web api 需要基本身份验证和 JSON 对象作为输入 (POST)。目前这个基本身份验证工作正常:
curl -i --user validuser:70e12a10-83c7-11e0-9d78-0800200c9a65 http://example.com/api.php
but I also want to send a JSON object as a POST request:
但我也想发送一个 JSON 对象作为 POST 请求:
curl -i --user validuser:70e12a10-83c7-11e0-9d78-0800200c9a65 -X POST -d '{"person":{"name":"bob"}}' http://example.com/api.php
I'm getting a 400 Bad Request response with the above command, any ideas on how I bundle a json object in this POST request?
我使用上述命令收到 400 Bad Request 响应,关于如何在此 POST 请求中捆绑 json 对象的任何想法?
回答by Jits
Try it with:
试试看:
curl -i --user validuser:70e12a10-83c7-11e0-9d78-0800200c9a65 -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"person":{"name":"bob"}}' http://mysite.com/api.php
I've removed the json=bit in the body content.
我已经删除json=了正文内容中的那一点。
Alternatively, this post might be helpful: How to post JSON to PHP with curl
或者,这篇文章可能会有所帮助:How to post JSON to PHP with curl
回答by 4F2E4A2E
curl --request POST \
--url http://host/api/content/ \
--header 'authorization: Basic Esdfkjhsdft4934hdfksjdf'
回答by Luca Matteis
Don't use
不要使用
$person = file_get_contents("php://input");
instead use
而是使用
$person = $_POST['person'];
And if you're using curl from the command-line this is the syntax for wanting to POST json data:
如果您从命令行使用 curl,这是想要 POST json 数据的语法:
curl -d 'person={"name":"bob"}'

