1761

我希望在 Linux 机器上向我的 Apache 服务器发送标头。如何通过 cURL 调用实现此目的?

4

11 回答 11

2087

man curl

   -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.

例子:

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

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

于 2008-12-10T16:41:02.087 回答
834

得到:

使用 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 帖子:

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

登录网站(auth):

curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/
于 2013-10-07T05:15:07.597 回答
291

PHP中:

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

或者您可以设置多个:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue', 'HeaderName2:HeaderValue2'));
于 2010-04-03T09:49:51.773 回答
60

使用-H or --header.

手册页:http ://curl.haxx.se/docs/manpage.html#-H

于 2008-12-10T16:41:37.810 回答
48

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"
于 2015-10-14T09:44:51.610 回答
25

我使用邮递员。

执行您想做的任何呼叫。然后,postman 提供了一个方便的工具来显示 curl 代码。

在终端中运行它。 在此处输入图像描述

在此处输入图像描述

于 2019-04-09T11:03:45.940 回答
24

您还可以发送多个标头、数据(例如 JSON),并将调用方法(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",
    }
  ]
}'
于 2019-01-07T20:34:58.467 回答
10

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

http http://myurl HeaderName:value
于 2015-09-02T20:39:59.003 回答
7

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

curl -v -H @{'custom_header'='custom_header_value'} http://localhost:3000/action?result1=gh&result2=ghk
于 2017-08-09T10:27:04.257 回答
5

在通过windows的anaconda环境中,命令应该是:GET,例如:

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

发布或修补数据,例如:

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.execurl不仅仅是为了避免这个问题:

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
于 2020-02-13T14:23:27.423 回答
3

以下是最常见的 http 方法的一些 curl 命令。

这里考虑的域对象是

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document
@Validated
public class Movie {
    @Id
    private String id;
    private String name;
    @NotNull
    private Integer year;
    @NotNull
    private 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
于 2022-01-12T13:17:05.713 回答