收到从POST请求下载文件时返回的415错误。

huangapple go评论63阅读模式
英文:

Getting 415 error from POST to download file

问题

以下是您提供的内容的翻译:

我正在使用Spring Rest来下载一个zip文件,其内容由一系列文档ID确定。

我的控制器如下所示:

@RestController
@RequestMapping("api/zip-documents")
public class DocumentsRestController {
    
    @Autowired
    private DocumentDownloadService documentDownloadService;
    
    @PostMapping(produces = {"application/zip"}, consumes = {"application/json"})
    public ResponseEntity<Resource> downloadZip(@RequestBody List<String> documentIds) throws IOException {
        // 将文档压缩到文件中
        ByteArrayOutputStream outputStream = documentDownloadService.downloadZip(documentIds);
        ByteArrayResource resource = new ByteArrayResource(outputStream.toByteArray());
        return ResponseEntity.ok().header("Content-Disposition", "attachment; filename=\"file.zip\"")
            .contentLength(outputStream.size()).body(resource);
    }
}

我的测试使用Mockito如下,运行应用程序时也遇到相同的问题:

@Test
public void downloadZip_sunnyDayUseCase_contentTypeIsZip() throws Exception {
    Mockito.when(documentDownloadService.downloadZip(Matchers.anyListOf(String.class)))
        .thenReturn(new ByteArrayOutputStream());

    mockMvc
        .perform(post("/api/zip-documents")
                .content("{ \"documentIds\": [\"123123\"] }"))
        .andExpect(status().isOk())
        .andExpect(content().contentType("application/zip"));
}

我得到了一个HttpStatus 415的响应。这似乎是请求头的问题,因为我无法在restcontroler中设置断点。

英文:

I am using Spring Rest to download a zip file, then contents of which are determined by a list of document ids.

My Controller is like so

@RestController
@RequestMapping(&quot;api/zip-documents&quot;)
public class DocumentsRestController {
    
    @Autowired
    private DocumentDownloadService documentDownloadService;
    
    @PostMapping(produces = {&quot;application/zip&quot;}, consumes = {&quot;application/json&quot;})
    public ResponseEntity&lt;Resource&gt; downloadZip(@RequestBody List&lt;String&gt; documentIds) throws IOException {
        // Zip the documents into a file
        ByteArrayOutputStream outputStream = documentDownloadService.downloadZip(documentIds);
        ByteArrayResource resource = new ByteArrayResource(outputStream.toByteArray());
        return ResponseEntity.ok().header(&quot;Content-Disposition&quot;, &quot;attachment; filename=\&quot;file.zip\&quot;&quot;)
            .contentLength(outputStream.size()).body(resource);
    }
}

My test, using Mockito is as follows, and I also get the same issue when running the app:

    @Test
    public void downloadZip_sunnyDayUseCase_contentTypeIsZip() throws Exception {
        Mockito.when(documentDownloadService.downloadZip(Matchers.anyListOf(String.class)))
            .thenReturn(new ByteArrayOutputStream());

        mockMvc
            .perform(post(&quot;/api/zip-documents&quot;)
                    .content(&quot;{ \&quot;documentIds\&quot;: [\&quot;123123\&quot;] }&quot;))
            .andExpect(status().isOk())
            .andExpect(content().contentType(&quot;application/zip&quot;));
    }

I'm getting a HttpStatus 415 response. This seems to be an issue with the request header, as I cannot hit a breakpoint in the restcontroler.

答案1

得分: 3

HTTP状态码415代表不支持的媒体类型。由于您正试图向端点发布JSON数据,您可能希望在POST请求中的某个位置添加Content-Type: application/json头部。

因此,在您的mockMvc中,我猜您可以尝试以下操作:

mockMvc
            .perform(post("/api/zip-documents")
                    .header("Content-Type", "application/json")
                    .content("{ \"documentIds\": [\"123123\"] }"))
            .andExpect(status().isOk())
            .andExpect(content().contentType("application/zip"));
英文:

HTTP Status Code 415 stands for Unsupported Media Type. Since you are trying to post JSON data to the endpoint, you probably want a Content-Type: application/json header somewhere in your POST.

So, in your mockMvc, I guess try the following:

mockMvc
            .perform(post(&quot;/api/zip-documents&quot;)
                    .header(&quot;Content-Type&quot;, &quot;application/json&quot;)
                    .content(&quot;{ \&quot;documentIds\&quot;: [\&quot;123123\&quot;] }&quot;))
            .andExpect(status().isOk())
            .andExpect(content().contentType(&quot;application/zip&quot;));

huangapple
  • 本文由 发表于 2020年8月29日 05:40:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63641189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定