简体   繁体   English

cURL - 设置 Content-Type 标头不起作用?

[英]cURL - Setting Content-Type Header is not working?

I'm trying to set the content-type header of my cURL request to application/json.我正在尝试将 cURL 请求的内容类型标头设置为 application/json。

This is the request I'm using in cmd (I left out the Json-Body):这是我在 cmd 中使用的请求(我省略了 Json-Body):

curl -v -X POST http://localhost:40071/api/Sale --header 'Content-Type: application/json' -d '{[Json Body]}'

When running this command, cURL is logging the following:运行此命令时,cURL 会记录以下内容:

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 127.0.0.1:40071...
* Connected to localhost (127.0.0.1) port 40071 (#0)
> POST /api/Sale HTTP/1.1
> Host: localhost:40071
> User-Agent: curl/7.79.1
> Accept: */*
> Content-Length: 332
> Content-Type: application/x-www-form-urlencoded
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< Date: Tue, 09 Nov 2021 11:18:33 GMT
< Content-length: 0
<

My REST server is running into an exception because of HTTP 415 Unsupported Media Type.由于 HTTP 415 Unsupported Media Type,我的 REST 服务器遇到异常。

It seems as if cURL did not make use of my "--header" parameter?好像 cURL 没有使用我的“--header”参数? Because the log is showing that Content-Type: application/x-www-form-urlencoded was chosen.因为日志显示选择了 Content-Type: application/x-www-form-urlencoded。

I also tried it with the abbreviated parameter "-h".我还尝试使用缩写参数“-h”。

Seems duplicate question, for more examples, refer here.似乎是重复的问题,有关更多示例,请参阅此处。

How to send a header using a HTTP request through a cURL call? 如何通过 cURL 调用使用 HTTP 请求发送标头?

curl -X POST mockbin.org/request -H "Accept: application/json"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM