使用cURL为自定义请求标头发送空字符串值

时间:2016-04-08 19:57:31

标签: c linux apache http curl

我正在尝试使用Linux机器上的cURL将值设置为“”的HTTP POST自定义请求标头(X-Privet-Token)发送。以下是我的命令:

curl -k -sI POST -H "X-Privet-Token:" -H "Content-Type: application/json" --trace-ascii --data "action=start&user=xyz@gmail.com" https://127.0.0.1/privet/register

我想将一个空字符串发送到服务器。

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

您可以使用以下语法将自定义标头设置为空值

curl -H "X-custom-header;" <some host>

在手册页中:

man curl
...
       -H, --header <header>
...
If you send the custom header with no-value then its header must be termi-
              nated with a semicolon, such as -H "X-Custom-Header;" to send "X-Custom-Header:".
...

答案 1 :(得分:0)

我知道这是一个老问题,但我认为值得回答。

我在Java客户端应用程序中调用 privet / info API调用时遇到了类似的问题,我意识到标头在空时没有发送。如果您想设置一个空标题,请尝试使用 \&#34; \&#34; (这是privet文档中针对/ privet / info API调用的内容):

curl -k -sI POST -H "x-privet-token: \"\"" -H "Content-Type: application/json" --trace-ascii --data "action=start&user=xyz@gmail.com" https://127.0.0.1/privet/register

另一方面, privet / register API调用需要有效的x-privet-token。将 x-privet-token 标题留空,&#39; curl&#39;可能是在遗漏它。

话虽如此,当端点没有宣传 / privet / register API时,会发生错误,可能是因为它已经注册了。您可以检查设备正在公开的API,检查 / privet / info 响应。