使用CURL POST与Json格式的有效负载



我正在本地服务器上使用CURL测试一个API。我需要使用CURL GET请求与Json参数。

我的Json将是这样的:{"key1": [{subkey1":"subvalue1"subkey2":"subvalue2"}),"key2": [{subkey3":"subvalue3"subkey4":"subvalue4"}]}我试过了:Curl -i http://localhost/test/api?key1

或将我的json文件保存为test.txt并使用curl -X POST -H 'Content-type:application/json'——data-binary '@D:/test.txt' http://localhost/test/api?key1

…但是它们都不工作....那么,如何使用curl创建带有效负载的post请求呢?谢谢!

我想获得返回值为Key1 = [***]Key2 = [####]

使用curl发送JSON有很多不同的方法。参考你的API文档来检查你应该使用哪个方法。

下面是一些常用的:

application/x-www-form-urlencodedPOST请求中发送json可以这样做:

curl http://example.com --data-urlencode json='{"foo":"bar"}'

请求看起来像:

POST / HTTP/1.1
Host: example.com
User-Agent: curl/7.87.0
Accept: */*
Content-Length: 32
Content-Type: application/x-www-form-urlencoded
json=%7B%22foo%22%3A%22bar%22%7D

json在multipart/form-dataPOST请求可以这样做:

curl http://example.com --form json='{"foo":"bar"}'

请求将看起来像

POST / HTTP/1.1
Host: example.com
User-Agent: curl/7.87.0
Accept: */*
Content-Length: 152
Content-Type: multipart/form-data; boundary=------------------------ca36a58e4d11c82e
--------------------------ca36a58e4d11c82e
Content-Disposition: form-data; name="json"
{"foo":"bar"}
--------------------------ca36a58e4d11c82e--

在POST请求中发送原始json可以这样做:

curl --header 'Content-Type: application/json' --request POST --data-binary '{"foo":"bar"}'

请求将看起来像

POST / HTTP/1.1
Host: example.com
User-Agent: curl/7.87.0
Accept: */*
Content-Type: application/json
Content-Length: 13
{"foo":"bar"}

相关内容

最新更新