如何通过cURL调用使用HTTP请求发送标头?

我希望在 Linux 上向我的 Apache 服务器发送一个带 header 的请求 。如何通过 cURL 调用实现此目的?

2486766 次浏览

man curl

   -H/--header 
(HTTP) Extra header to use when getting a web page. You may specifyany number of extra headers. Note that if you should add a customheader that has the same name as one of the internal ones curl woulduse, your externally set header will be used instead of the internalone. This allows you to make even trickier stuff than curl wouldnormally do. You should not replace internally set headers withoutknowing perfectly well what you're doing. Remove an internal headerby giving a replacement without content on the right side of thecolon, as in: -H "Host:". curl will make sure that each header you add/replace get sent withthe proper end of line marker, you should thus not add that as apart of the header content: do not add newlines or carriage returnsthey will only mess things up for you. See also the -A/--user-agent and -e/--referer options. This option can be used multiple times to add/replace/remove multi-ple headers.

示例1:单个 header

curl --header "X-MyHeader: 123" www.google.com

示例2:多个标头

curl --header "Accept: text/javascript" --header "X-Test: hello" -v www.google.com

您可以通过添加-v选项查看curl发送的请求。

php

curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue'));

或者您可以设置多个:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue', 'HeaderName2:HeaderValue2'));

获取:

使用JSON:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://hostname/resource

使用XML:

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

帖子:

发布数据:

curl --data "param1=value1&param2=value2" http://hostname/resource

文件上传:

curl --form "fileupload=@filename.txt" http://hostname/resource

RESTful HTTP Post:

curl -X POST -d @filename http://hostname/resource

登录到一个站点(auth):

curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Logincurl -L -b headers http://localhost/

我已经从 curl 切换到 Httpie;语法如下所示:

http http://myurl HeaderName:value

GET(多个参数):

curl -X  GET "http://localhost:3000/action?result1=gh&result2=ghk"

curl --request  GET "http://localhost:3000/action?result1=gh&result2=ghk"

curl  "http://localhost:3000/action?result1=gh&result2=ghk"

curl -i -H "Application/json" -H "Content-type: application/json"  "http://localhost:3000/action?result1=gh&result2=ghk"

如果你想发送你的自定义标头,你可以这样做:

curl -v -H @{'custom_header'='custom_header_value'} http://localhost:3000/action?result1=gh&result2=ghk

您还可以将多个标头、数据(例如JSON)和指定Call方法(POST、GET)发送到单个CUrl调用中,如下所示:

curl -X POST(Get or whatever) \http://your_url.com/api/endpoint \-H 'Content-Type: application/json' \-H 'header-element1: header-data1' \-H 'header-element2: header-data2' \

……更多标题…………

  -d '{"JsonExArray": [{"json_prop": "1",},{"json_prop": "2",}]}'

我用 Postman。

然后,postman提供了一个方便的工具来显示 curl 代码。

在终端中运行它。输入图片描述

在此处输入图片描述

anacondawindows中,命令应该是:GET:

curl.exe http://127.0.0.1:5000/books

Post 或者 Patch 对应的数据:

curl.exe http://127.0.0.1:5000/books/8 -X PATCH -H "Content-Type: application/json" -d '{\"rating\":\"2\"}'

PS:为json数据添加反斜杠以避免这种错误=>Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)

使用curl.exe而不是curl只是为了避免这个问题:

Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "Content-Type: application/json" value of type"System.String" to type "System.Collections.IDictionary".At line:1 char:48+ ... 0.1:5000/books/8 -X PATCH -H "Content-Type: application/json" -d '{\" ...+                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

以下是最常见的超文本传输协议方法的一些curl命令。

这里考虑的域对象是

@Data@NoArgsConstructor@AllArgsConstructor@Document@Validatedpublic class Movie {@Idprivate String id;private String name;@NotNullprivate Integer year;@NotNullprivate List<String> cast;private LocalDate release_date;}

创建后电影

curl -i \-d '{"id":1, "name": "Dhoom", "year":2004,"cast":["John Abraham", "Abhishek Bachan"],"release_date": "2004-06-15"}' \-H "Content-Type: application/json" \-X POST http://localhost:8080/v1/movies

获取所有电影

curl -i http://localhost:8080/v1/movies

获取电影ID

curl -i http://localhost:8080/v1/movies/1

播放-更新-影片

curl -i \-d '{"id":1, "name": "Dhoom", "year":2005,"cast":["John Abhraham", "Abhishek Bachhan", "Uday Chopra", "Isha Deol"],"release_date": "2005-03-25"}' \-H "Content-Type: application/json" \-X PUT http://localhost:8080/v1/movies/1

删除影片

curl -i -X DELETE http://localhost:8080/v1/movies/1