How to determine Content Type of a HTTP Servlet Request?

自闭症网瘾萝莉.ら 提交于 2019-12-08 02:11:30

问题


How can I get the content type from the HttpServletRequest without reading the request body?

When I use the following, I get null:

request.getContentType()

When I try to read the JSON data that comes in the request body using the following:

StringBuilder jsonsb = new StringBuilder();
BufferedReader jsonbr = request.getReader();

The request.getReader() throws

Caused by: java.lang.NullPointerException: null
    at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:106)

I even tried using the following and was able to get the content type but, still getting the same NullPointerException while getting the reader from request after this statement.

request.getHeader("Accept")

回答1:


Are you handling a GET request. That might not contain a Content-Types header and therefore you are seeing a null there.



来源:https://stackoverflow.com/questions/30049094/how-to-determine-content-type-of-a-http-servlet-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!