我使用Ubuntu,并在上面安装了cURL.我想用cURL测试我的Spring睡觉应用程序.我在Java端编写了我的邮政编码.不过,我想用curl来测试它.我正在try 发布一个JSON数据.示例数据如下所示:

{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}

我使用这个命令:

curl -i \
    -H "Accept: application/json" \
    -H "X-HTTP-Method-Override: PUT" \
    -X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true \
    http://localhost:8080/xx/xxx/xxxx

It returns this error:

HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1051
Date: Wed, 24 Aug 2011 08:50:17 GMT

The error description is this:

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().

Tomcat日志(log):

cURL命令的正确格式是什么?

This is my Java side PUT code (I have tested GET and DELETE and they work):

@RequestMapping(method = RequestMethod.PUT)
public Configuration updateConfiguration(HttpServletResponse response, @RequestBody Configuration configuration) { //consider @Valid tag
    configuration.setName("PUT worked");
    //todo If error occurs response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return configuration;
}

推荐答案

您需要将内容类型设置为application/json.但-d(或--data)发送的内容类型为application/x-www-form-urlencoded,Spring不接受.

Looking at the curl man page, I think you can use -H (or --header):

-H "Content-Type: application/json"

Full example:

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"xyz","password":"xyz"}' \
  http://localhost:3000/api/login

(-H代表--header-d代表--data)

Note that -request POST is optional if you use -d, as the -d flag implies a POST request.


在Windows上,情况略有不同.查看 comments 帖子.

Json相关问答推荐

Jolt Change将对象添加到数组

使用jolt删除空对象

手动解开没有可编码的SON- Swift

如何在Power BI中集成API和JSON数据后将数据转换为表?

如何使用 JOLT 将带有列表的 JSON 项目取消列出为多个项目?

jq :当路径可变时更新 json 内容

如何使用 serde_json 构建有状态的流式解析器?

xidel:是否可以从 JSON 对象中检索特定的嵌套值?

Nifi - 忽略(或删除)JSON 的第一个数字

如何在linux中用jq过滤json数组?

boost::json::value 的大括号初始化将其从对象转换为数组

C# 合并 2 个几乎相同的 JSON 对象

如何比较 JSON 文档并返回与 Jackson 或 Gson 的差异?

如何从 JSON 对象中获取日期

在 Apache Spark 中读取多行 JSON

在 JavaScript 中从 Json 数据中删除反斜杠

错误未判断调用put(K, V)作为原始类型java.util.HashMap的成员

如何向从文件中检索的 JSON 数据添加键值?

带有 Jackson 的不可变 Lombok 注释类

Volley JsonObjectRequest Post 参数不再起作用