OkObjectResult with ContentTypes "application/pdf" 邮递员如何收到 406 Not Acceptable
OkObjectResult with ContentTypes "application/pdf" some how is received by postman as 406 Not Acceptable
使用 Postman,我向我的服务发送一个 GET 请求,其中包含 Header 以接受“application/pdf”和 Accept-Encoding“gzip、deflate、br、*”。
在代码方面,
byte[] pdf_test = System.IO.File.ReadAllBytes(@"C:\somewhere\hello.pdf");
OkObjectResult output = new OkObjectResult(pdf_test); // uncomment below once done.
output.ContentTypes.Add(@"application/pdf"); // LINE IN QUESTION
// And finally, back to the remote caller.
return output;
当我调试此代码时,状态代码为 200,并且 ContentTypes
具有“aplication/pdf”。具有 pdf 的所有字节的值。
当邮递员收到回复时,我看到 return 代码为 406 不可接受。
奇怪的是,当我从上面注释掉“LINE IN QUESTION”时,邮递员收到 200 OK 的消息。但是 Content-Type 接受为“application/json”。
为什么会这样?
Postman 最终无关紧要,但真正的程序需要“application/pdf”内容类型,这个真正的程序还不存在。所以至少我想把我这边做好。
提前致谢。
要return一个文件的内容,你最好使用FileStreamResult
.
var stream = System.IO.File.OpenRead(@"C:\somewhere\hello.pdf");
return new FileStreamResult(stream, "application/pdf");
OkObjectResult
参与内容协商和格式化。
来自文档:
An ObjectResult that when executed performs content negotiation,
formats the entity body, and will produce a Status200OK response if
negotiation and formatting succeed.
这不应该发生在这里return一个byte-array/stream。
source code at GitHub 显示 HTTP
状态代码 406 Not Acceptable
在执行 OkResultObject
时设置,因为不存在格式化程序(对于内容类型 application/pdf
).
使用 Postman,我向我的服务发送一个 GET 请求,其中包含 Header 以接受“application/pdf”和 Accept-Encoding“gzip、deflate、br、*”。
在代码方面,
byte[] pdf_test = System.IO.File.ReadAllBytes(@"C:\somewhere\hello.pdf");
OkObjectResult output = new OkObjectResult(pdf_test); // uncomment below once done.
output.ContentTypes.Add(@"application/pdf"); // LINE IN QUESTION
// And finally, back to the remote caller.
return output;
当我调试此代码时,状态代码为 200,并且 ContentTypes
具有“aplication/pdf”。具有 pdf 的所有字节的值。
当邮递员收到回复时,我看到 return 代码为 406 不可接受。
奇怪的是,当我从上面注释掉“LINE IN QUESTION”时,邮递员收到 200 OK 的消息。但是 Content-Type 接受为“application/json”。
为什么会这样?
Postman 最终无关紧要,但真正的程序需要“application/pdf”内容类型,这个真正的程序还不存在。所以至少我想把我这边做好。
提前致谢。
要return一个文件的内容,你最好使用FileStreamResult
.
var stream = System.IO.File.OpenRead(@"C:\somewhere\hello.pdf");
return new FileStreamResult(stream, "application/pdf");
OkObjectResult
参与内容协商和格式化。
来自文档:
An ObjectResult that when executed performs content negotiation, formats the entity body, and will produce a Status200OK response if negotiation and formatting succeed.
这不应该发生在这里return一个byte-array/stream。
source code at GitHub 显示 HTTP
状态代码 406 Not Acceptable
在执行 OkResultObject
时设置,因为不存在格式化程序(对于内容类型 application/pdf
).