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

How to send a header using a HTTP request through a curl call?

我希望在Linux设备上向我的Apache服务器发送一个头文件。我怎样才能通过一个curl调用实现这一点?


man curl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   -H/--header <header>
          (HTTP)  Extra header to use when getting a web page. You may specify
          any number of extra headers. Note that if you should  add  a  custom
          header that has the same name as one of the internal ones curl would
          use, your externally set header will be used instead of the internal
          one.  This  allows  you  to make even trickier stuff than curl would
          normally do. You should not replace internally set  headers  without
          knowing  perfectly well what you're doing. Remove an internal header
          by giving a replacement without content on the  right  side  of  the
          colon, as in: -H"Host:".

          curl  will  make sure that each header you add/replace get sent with
          the proper end of line marker, you should thus not  add  that  as  a
          part  of the header content: do not add newlines or carriage returns
          they 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
curl --header"X-MyHeader: 123" www.google.com

你可以看到这是由卷曲的请求被添加-v选项。


得到:

带格式的:

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

与XML

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

职务:

如果发布日期:

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

文件上传

1
curl --form"[email protected]" http://hostname/resource

RESTful HTTP POST:

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

为记录到的网站:(认证)

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


在PHP

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

或者你可以设置多个。

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


使用-H or --header

城域网:http://curl.haxx.se页/文档/ manpage.html # H


get(多参数):

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

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

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

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


我对httpie开关从卷曲;语法看起来像:

1
http http://myurl HeaderName:value


你要把你的情况,你可以自定义的标题是这样的:

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

你还可以发送多个标题,日期(例如,JSON)和指定的呼叫的方法(POST,GET)卷曲成这样:呼叫单

1
2
3
4
5
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' \

标题……………………………

1
2
3
4
5
6
7
8
9
10
  -d '{
 "JsonExArray": [
    {
     "json_prop":"1",
    },
    {
     "json_prop":"2",
    }
  ]
}'

我使用的邮递员。

无论你想进行呼叫。然后,提供了方便的工具给邮差的卷曲的代码。

它运行在终端。enter image description here

enter image description here