Enonic 6.9.x - How to create a service that accepts request body as JSON

Enonic version: Enonic 6.9.x
OS: Centos 6

Hi,

I’m trying to create a service controller that should handle request body as Json from a call from a client.
exports.post = function(req) {

return {
	body: {
		result: "CALLBACK_RECEIVED"
	},
	contentType: 'application/json; charset=utf-8'
};

};

When I try to call this service with POSTMAN tool with:
method: POST
HEADERS:
Content-Type: application/json
request body/body content as json:
{“endPoint”:null,“orderId”:“VT1472048925”,“siteId”:null,“transactionInfo”:{“amount”:100,“timeStamp”:“2014-06-24T08:34:25-07:00”,“status”:“RESERVED”,“transactionId”:“101344195”},“errorInfo”:null}

I got server error.

My question is: could you point me some guide on how to create a service controller that will handle this kind of call from client?

Thanks

Your code looks correct. Is there any exception error in the log when you make the request?

To receive the posted JSON in the controller, just add this line inside the function:

var requestJson = JSON.parse(req.body);
1 Like

The incoming json request body is valid:
{
“orderId”: “VT1472048925”,
“transactionInfo”: {
“amount”: 100,
“timeStamp”: “2014-06-24T08:34:25-07:00”,
“status”: “RESERVED”,
“transactionId”: “101344195”
}
}

It seems that my service can’t handle the json request body. I got this error:

Caused by: java.lang.IllegalArgumentException: Could not parse ‘application/x-www-form-urlencoded, application/json’
at com.google.common.net.MediaType.parse(MediaType.java:629) ~[guava-18.0.jar:na]
at com.enonic.xp.web.impl.serializer.RequestBodyReader.readBody(RequestBodyReader.java:23) ~[na:na]
at com.enonic.xp.web.impl.handler.WebDispatcherServlet.newWebRequest(WebDispatcherServlet.java:84) ~[na:na]
at com.enonic.xp.web.impl.handler.WebDispatcherServlet.service(WebDispatcherServlet.java:59) ~[na:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) ~[web-jetty-6.10.1.jar:6.10.1]
at com.enonic.xp.web.impl.dispatch.mapping.ServletDefinitionImpl.service(ServletDefinitionImpl.java:39) ~[na:na]
at com.enonic.xp.web.impl.dispatch.pipeline.ServletPipelineImpl.service(ServletPipelineImpl.java:30) ~[na:na]
at com.enonic.xp.web.impl.dispatch.pipeline.FilterChainImpl.doFilter(FilterChainImpl.java:45) ~[na:na]
at com.enonic.xp.web.impl.dispatch.pipeline.FilterChainImpl.doFilter(FilterChainImpl.java:36) ~[na:na]
at com.enonic.xp.portal.impl.auth.AuthFilter.doHandle(AuthFilter.java:45) ~[na:na]
at com.enonic.xp.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:31) ~[na:na]
at com.enonic.xp.web.filter.BaseWebFilter.doFilter(BaseWebFilter.java:33) ~[na:na]
… 54 common frames omitted
Caused by: java.lang.IllegalStateException: null
at com.google.common.base.Preconditions.checkState(Preconditions.java:158) ~[guava-18.0.jar:na]
at com.google.common.net.MediaType$Tokenizer.consumeCharacter(MediaType.java:665) ~[guava-18.0.jar:na]
at com.google.common.net.MediaType.parse(MediaType.java:604) ~[guava-18.0.jar:na]
… 65 common frames omitted

The problem is that the client is sending a Content-Type header that is invalid.

From the exception, the client sends this header when it makes the request:

  • Content-Type: application/x-www-form-urlencoded, application/json

The Content Type can only have one value, in this case it’s saying that is both form-urlencoded and json at the same time.

Check the client code, in this case it should have set the header as: Content-Type: application/json

2 Likes

You are right.
For some reason, the HTTP Tool addon in firefox appended “application/x-www-form-urlencoded” to content type “application/json”
I tried RESTClient addon in firefox and it works.

Thank you very much :slight_smile:

1 Like