Search code examples
jsfpostdatafacescontext

How to retrieve posted data via FacesContext


I'm making the following curl request to a legacy JSF application:

curl -H "Accept: application/json" -H "Content-type: text/plain" -X POST \
-d '{"name":"value"}' http://localhost:8080/aaa/bbb

The problem I'm having is I don't know how to retrieve the posted JSON data from the FacesContext as a raw String.

I've tried the following:

FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()

and this returns an empty map, which I'm assuming is because the posted data is not keyed to a parameter name.

If I debug with the following:

FacesContext.getCurrentInstance().getExternalContext().getRequestContentLength()

I get the correct data length, i.e. 16

If I debug with the following:

FacesContext.getCurrentInstance().getExternalContext().getRequestContentType()

I get the correct type, i.e. text/plain.

But what I can't work out is how to actually retrieve the posted data itself.

Any help would be appreciated.

Kind regards


Solution

  • You can call the getReader() method of the HttpServletRequest class to obtain a BufferedReader that'll allow you to read the POST content data.

    ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getReader()