【问题标题】:curl -d "123" : The string data has equal symbol attached to itcurl -d "123" :字符串数据附加了等号
【发布时间】:2022-07-01 21:47:16
【问题描述】:

我在 Spring Boot Restcontroller 中有一个 POST API,它接受一个字符串并返回该字符串。但是收到的字符串值具有特殊字符,例如“=”

@RestController
public class MyApi{
    @PostMapping(path = "/", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public String parseInput(@RequestBody String data)    {
        return data;
    }
}

curl -d "1" http://localhost:1337/ 给出1= 而不是1

【问题讨论】:

标签: java spring-boot rest


【解决方案1】:

当使用-d "1"curl 时,您尝试:

[发送] POST 请求中的指定数据到 HTTP 服务器,就像浏览器在用户填写 HTML 表单并按下提交按钮时所做的一样。这将导致 curl 使用 content-type application/x-www-form-urlencoded 将数据传递给服务器。

这实质上意味着负载被视为键值对,由 & 字符分隔,而键和值由等号 (=) 分隔。

在您的情况下,1 被视为键并且其值丢失,因此您收到的 1=

如果您只想使用纯文本,您需要:

  1. 更改consumes = {MediaType.TEXT_PLAIN}
  2. 使用curl -H "Content-Type: text/plain; charset: utf-8" -d "1" ...

【讨论】:

  • 我可以知道如何修复上述 API 并使其按预期工作吗?
  • 我更新了我的答案@AnthonyVinay
猜你喜欢
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多