Curl在发送表单时附加到Content-Type,如何覆盖它?

时间:2017-12-13 00:45:44

标签: url curl header mime-types

举个例子:

curl http://host.compute-1.amazonaws.com:8080/oauth2/token -H "Content-Type: application/x-www-form-urlencoded" -F 'grant_type=password' -F 'username=test-user' -F 'password=password' -F 'client_id=test_application_client_id' -F 'client_secret=password' -v

有人会认为Content-Type会设置为application/x-www-form-urlencoded。而是Curl发送application/x-www-form-urlencoded; boundary=------------------------5f55da42226c00e5

有没有办法这样做?

jonathanfisher@odin ~/ $ curl http://something.compute-1.amazonaws.com:8080/oauth2/token -H "Content-Type: application/x-www-form-urlencoded" -F 'grant_type=password' -F 'username=test-user' -F 'password=password' -F 'client_id=test_application_client_id' -F 'client_secret=password' -v
*   Trying 0.0.0.0...
* TCP_NODELAY set
* Connected to something.compute-1.amazonaws.com (0.0.0.0) port 8080 (#0)
> POST /oauth2/token HTTP/1.1
> Host: something.compute-1.amazonaws.com:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 598
> Expect: 100-continue
> Content-Type: application/x-www-form-urlencoded; boundary=------------------------5f55da42226c00e5
> 
< HTTP/1.1 100 Continue
< HTTP/1.1 400 Bad Request
< Server: Apache-Coyote/1.1
< Date: Wed, 13 Dec 2017 00:43:13 GMT
< Content-Type: text/plain
< Content-Length: 145
< Connection: close
< 
* Closing connection 0
WebApplicationException has been caught, status: 400, message: The Content-Type header is missing on this request you should define: Content-Type

1 个答案:

答案 0 :(得分:1)

来自curl manpage:

  

-F, - 表格                 (HTTP)这使curl模拟用户按下提交按钮的填写表单。这导致curl POST   根据RFC 2388使用Content-Type multipart / form-data的数据。                 这样可以上传二进制文件等。

您是否尝试将-F替换为-d

以下是我对两者的看法:

使用-F

$ curl -F "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://google.com -v
* About to connect() to google.com port 80 (#0)
*   Trying 2a00:1450:400a:807::200e...
* Connected to google.com (2a00:1450:400a:807::200e) port 80 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: google.com
> Accept: */*
> Content-Length: 161
> Expect: 100-continue
> Content-Type: application/x-www-form-urlencoded; boundary=----------------------------5bb04ed0c4fe
> 

使用-d

$ curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://google.com -v
* About to connect() to google.com port 80 (#0)
*   Trying 2a00:1450:400a:807::200e...
* Connected to google.com (2a00:1450:400a:807::200e) port 80 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: google.com
> Accept: */*
> Content-Type: application/x-www-form-urlencoded
> Content-Length: 27
>