【问题标题】:Sending raw data in request body在请求正文中发送原始数据
【发布时间】:2018-12-19 14:59:10
【问题描述】:

我遇到了将请求正文作为原始文本发送不带引号的问题。

请求的内容类型应该是text/uri-list

在 Postman 中发送它可以正常工作,但是当我尝试在 java 中实现相同的操作时它不起作用。

我使用 feign 作为 api 客户端。

端点的客户端定义如下所示

@RequestLine("PUT /someEndpointOne/{id}/someEndpointTwo")
@Headers("Content-Type: text/uri-list")
JSONObject addSomethingToSomething(@Param("id") String id, @RequestBody okhttp3.RequestBody uri); 

我在测试中这样使用它:

somethingClient.addSomethingToSomething("1", okhttp3.RequestBody.create(okhttp3.MediaType.parse("text/uri-list"), "http://localhost/someEndpointTwo/1"))

它实际上发送的是空对象,而不是发送原始数据:

PUT http://localhost/someEndpointOne/1/someEndpointTwo HTTP/1.1

内容类型:文本/uri-list

内容长度:2

{}

END HTTP(2 字节正文)

导致不良反应的原因。

如果能帮助我解决这个问题,我将不胜感激。

【问题讨论】:

    标签: java spring api http netflix-feign


    【解决方案1】:

    以下对我有用:

    @PutMapping(path = "/someEndpointOne/{id}/someEndpointTwo", headers = "Content-Type=text/uri-list")
    JSONObject addSomethingToSomething(@PathVariable("id") String id, @RequestBody okhttp3.RequestBody uri); 
    

    其中@PutMapping 来自org.springframework.web.bind.annotation.PutMapping@PathVariable 来自org.springframework.web.bind.annotation.PathVariable

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 2022-01-22
      • 2021-03-04
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多