如何使用“filter[name]=foo”格式的查询字符串参数进行curl

问题描述 投票:0回答:2

这是我服务器上的 api:

https://server/api/v1/products?filter[name]=foo

当我通过浏览器访问它时效果很好,并且使用以下两种方法效果很好:

$ cat /tmp/x
{"filter": {"name": "foo"}}

$ curl -X GET -H 'Accept: application/json' -k -s --negotiate -u : -H 'Content-Type: application/json' -d "@/tmp/x" https://server/api/v1/products
{"name": "foo", ...}

$ curl -X GET -H 'Accept: application/json' -k -s --negotiate -u : -d "filter[name]=foo" https://server/api/v1/products
{"name": "foo", ...}

现在我正在尝试这样做:

$ curl -X GET -H 'Accept: application/json' -k -s --negotiate -u : https://server/api/v1/products?filter[name]=foo
$ echo $?
3

它失败并显示错误代码 3,它代表“URL 格式错误。语法不正确。”

如何在url中指定查询字符串参数?

http curl
2个回答
4
投票

查询字符串需要进行 URL 编码,因此它必须如下所示:

https://server/api/v1/products?filter%5Bname%5D=foo

asciitable.com.


0
投票

与此无关,但因为我最初是在寻找自己的问题时来到这里的。如果您在本地使用此过滤器进行卷曲,则可能需要确保使用的是

http
而不是
https

这种格式对我有用:

curl http://localhost:3000/api/v1/asset_passwords\?archived\=true\&page\=1\&page_size\=10 -H "x-api-key: [YOUR_API_KEY_HERE]
© www.soinside.com 2019 - 2024. All rights reserved.