我希望在 Linux 机器上向我的 Apache 服务器发送标头。如何通过 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.
示例 1:单个标题
curl --header "X-MyHeader: 123" www.google.com
示例 2:多个标题
curl --header "Accept: text/javascript" --header "X-Test: hello" -v www.google.com
您可以通过添加 -v
选项查看 curl 发送的请求。
得到:
使用 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¶m2=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/
Accept
用于希望拥有此功能的 客户端(询问/请求),但标头 Content-Type
只是服务器 回答仅此而已,并没有误会客户的愿望:“我想要这种类型的内容”。正确的?所以对于 GET curl -i -H "Accept: application/json" http://hostname/resource
应该是它。我错了吗?请参阅 developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type «在响应中,Content-Type
标头告诉客户端返回内容的实际内容类型。»
Content-Type
。不是请求。 Content-Type
总是指两方之间传输的数据类型。如果它在请求标头上,则意味着客户端说“是的,我正在向您发送数据类型 application/json
”到服务器。如果它正在响应,则意味着服务器向客户端说“现在我正在向您发送数据类型 text/plain
”。
在 PHP 中:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue'));
或者您可以设置多个:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue', 'HeaderName2:HeaderValue2'));
使用 -H or --header
。
手册页:http://curl.haxx.se/docs/manpage.html#-H
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"
我使用邮递员。
执行您想做的任何呼叫。然后,postman 提供了一个方便的工具来显示 curl 代码。
https://i.stack.imgur.com/ByU2a.png
https://i.stack.imgur.com/7jlsh.png
您还可以发送多个标头、数据(例如 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",
}
]
}'
如果您想发送自定义标头,您可以这样做:
curl -v -H @{'custom_header'='custom_header_value'} http://localhost:3000/action?result1=gh&result2=ghk
{'custom_header'='custom_header_value'}
的文件中读取标题吗?
在通过 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.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
以下是最常见的 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