【发布时间】:2020-07-22 11:35:10
【问题描述】:
我开发了一个 REST 端点,它可以生成一个序列化为字节数组的 zip 文件。
@GetMapping(path = "/export-zip", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<byte[]> exportZipFile() throws IOException {
try {
MyZipObject zip = zipService.createZip();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + zip.getFileName() + ".zip\"");
return new ResponseEntity<>(zip.getData(), httpHeaders, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
使用 Postman,我可以成功地从这个 API 获取响应并将其保存到 Zip 文件中。然而,我们的前端用来调用这个 API 的 React 代码会产生错误
Refused to get unsafe header “Content-Disposition"
我不确定为什么一开始就认为标头不安全,但我也想知道如何将其呈现为安全标头。
【问题讨论】:
标签: java reactjs spring-boot rest http-headers