Is it possible to send two different Content-Type for POST method using post-man? Like, if the service is used for downloading excel file so in that case, @Consumes(MediaType.APPLICATION_JSON)
is used for sending some user detail which is json structure and @Produces(MediaType.APPLICATION_OCTET_STREAM)
is used for sending back the response as file. NOTE: I don't want to use form-data, that is a constraint for this service.
是否可以使用post-man为POST方法发送两种不同的Content-Type?比如,如果该服务用于下载excel文件,那么在这种情况下,@ Consumes(MediaType.APPLICATION_JSON)用于发送一些用户详细信息,这是json结构,而@Produces(MediaType.APPLICATION_OCTET_STREAM)用于将响应发送回文件。注意:我不想使用表单数据,这是此服务的约束。
1 个解决方案
#1
1
On the client request, the Content-Type
header is only for the type of data that is in the entity body. The Accept
header is what you send for the type of data you want back.
在客户端请求中,Content-Type标头仅适用于实体主体中的数据类型。 Accept标头是您要发送的数据类型。
On the server side, it is perfectly fine to do the following.
在服务器端,完成以下操作是完全正常的。
@POST
@Path("something")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response post(Model model) throws Exception {
final InputStream in = new FileInputStream("path-to-file");
StreamingOutput entity = new StreamingOutput() {
@Override
public void write(OutputStream out) {
IOUtils.copy(in, out);
out.flush();
}
};
return Response.ok(entity)
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=somefile.xls")
.build();
}
The request to this endpoint should look like
对此端点的请求应如下所示
POST /api/something HTTP 1.1
Content-Type: application/json;charset=utf-8
Accept: application/octet-stream
{"the":"json", "entity":"body"}
See also:
-
This post about purpose of
@Produces
and@Consumes
and the role they play in content negotiation.
这篇文章讲述了@Produces和@Consumes的目的以及它们在内容协商中的作用。
Aside
As an aside, consider using application/vnd.ms-excel
as the Content-Type
instead of application/octet-stream
. This is already the standard MIME type for Microsoft Excel files1. When using StreamingOutput
as the response entity type, you can make the @Produces
anything you want, as there is no real conversion needed.
另外,考虑使用application / vnd.ms-excel作为Content-Type而不是application / octet-stream。这已经是Microsoft Excel文件1的标准MIME类型。当使用StreamingOutput作为响应实体类型时,您可以将@Produces设置为您想要的任何内容,因为不需要真正的转换。
In Postman, when you use the "Send and download" feature, you will notice that when the Content-Type
is application/octet-stream
, it will suggest the file extension of .bin
when saving the file. But if you have the Content-Type
as application/vnd.ms-excel
, then the suggested extension is .xls
, which is what it should be.
在Postman中,当您使用“发送和下载”功能时,您会注意到当Content-Type是application / octet-stream时,它会在保存文件时建议.bin的文件扩展名。但是如果你有Content-Type作为application / vnd.ms-excel,那么建议的扩展名是.xls,这应该是它。
1. Incomplete list of MIME types
1.不完整的MIME类型列表
#1
1
On the client request, the Content-Type
header is only for the type of data that is in the entity body. The Accept
header is what you send for the type of data you want back.
在客户端请求中,Content-Type标头仅适用于实体主体中的数据类型。 Accept标头是您要发送的数据类型。
On the server side, it is perfectly fine to do the following.
在服务器端,完成以下操作是完全正常的。
@POST
@Path("something")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response post(Model model) throws Exception {
final InputStream in = new FileInputStream("path-to-file");
StreamingOutput entity = new StreamingOutput() {
@Override
public void write(OutputStream out) {
IOUtils.copy(in, out);
out.flush();
}
};
return Response.ok(entity)
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=somefile.xls")
.build();
}
The request to this endpoint should look like
对此端点的请求应如下所示
POST /api/something HTTP 1.1
Content-Type: application/json;charset=utf-8
Accept: application/octet-stream
{"the":"json", "entity":"body"}
See also:
-
This post about purpose of
@Produces
and@Consumes
and the role they play in content negotiation.
这篇文章讲述了@Produces和@Consumes的目的以及它们在内容协商中的作用。
Aside
As an aside, consider using application/vnd.ms-excel
as the Content-Type
instead of application/octet-stream
. This is already the standard MIME type for Microsoft Excel files1. When using StreamingOutput
as the response entity type, you can make the @Produces
anything you want, as there is no real conversion needed.
另外,考虑使用application / vnd.ms-excel作为Content-Type而不是application / octet-stream。这已经是Microsoft Excel文件1的标准MIME类型。当使用StreamingOutput作为响应实体类型时,您可以将@Produces设置为您想要的任何内容,因为不需要真正的转换。
In Postman, when you use the "Send and download" feature, you will notice that when the Content-Type
is application/octet-stream
, it will suggest the file extension of .bin
when saving the file. But if you have the Content-Type
as application/vnd.ms-excel
, then the suggested extension is .xls
, which is what it should be.
在Postman中,当您使用“发送和下载”功能时,您会注意到当Content-Type是application / octet-stream时,它会在保存文件时建议.bin的文件扩展名。但是如果你有Content-Type作为application / vnd.ms-excel,那么建议的扩展名是.xls,这应该是它。
1. Incomplete list of MIME types
1.不完整的MIME类型列表