cURL GET查询字符串是JSON

use*_*791 4 json curl http

我尝试使用cURL访问远程RESTful Services API,查询字符串参数具有JSON值。我的cURL命令如下:

curl -g -i 'http://localhost:8080/context/restdev/employees/?q={"deptno":{"$lte":20}}'
Run Code Online (Sandbox Code Playgroud)

如果我在Chrome中输入URL,则可以取回数据。我的cURL命令得到:URI格式不正确,原因:查询位置中的非法字符:48

Ram*_*ose 6

您的查询

q={"deptno":{"$lte":20}}'
Run Code Online (Sandbox Code Playgroud)

需要进行百分比编码。cURL使用--data-urlencode开关支持该功能,因此请尝试以下操作(一行):

curl -G -i "http://localhost:8080/context/restdev/employees/" 
--data-urlencode 'q={"deptno":{"$lte":20}}'
Run Code Online (Sandbox Code Playgroud)