Search code examples
ibm-mobilefirstmobilefirst-adaptersmobilefirst-server

IBM MobileFirst Sending JSON Body from iOS SDK to Java Adapter


Using IBM Mobile First PlatForm Version 7.1 I am trying to call a Java Adapter from iOS Application using following code.

    [[WLResourceRequest requestWithURL:url method:WLHttpMethodPost] sendWithJSON:@{@"one":@"two"} completionHandler:^(WLResponse *response, NSError *error) {

    NSLog(@"%@",response);
    NSLog(@"%@",error);

    }];

Java Method on adapter side looks as following.

@POST
@Consumes("application/json")
@Produces("application/json")
public String hello(JSONObject body){
    return body.toString();
}

But I get Following error in response

2016-04-20 11:31:15.520 mbs-call[15092:3787968] Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)" UserInfo={com.alamofire.serialization.response.error.response= { URL: http:/0.0.0.0:10080/mbs-api/adapters/basicadpt/users } { status code: 415, headers { Connection = Close; "Content-Language" = "en-US"; "Content-Length" = 0; Date = "Wed, 20 Apr 2016 02:31:15 GMT"; "X-Powered-By" = "Servlet/3.0"; } }, NSErrorFailingURLKey=http://0.0.0.0:10080/mbs-api/adapters/basicadpt/users, com.alamofire.serialization.response.error.data=<>, NSLocalizedDescription=Request failed: unsupported media type (415)}

And it seems that in iOS SDK it adds header application/x-www-url-form-urlencoded in request when any method is called.

I have following 2 questions.

  1. How to pass JSON Body to Java adapter?
  2. Behaviour of sendWithJSON is different on iOS and Android. On Android it seems to add application/json header when we call adapter. Is that a bug or part of behaviour?

Solution

  • I believe this is a bug. I think that using sendWithJSON should automatically assume that the content type is application/json.

    I suggest that you open a support request (PMR) so that they can improve the experience.

    In the meantime, I found an easy workaround:

    [request addHeaderValue:@"application/json" forName:@"Content-Type"]
    

    Or in Swift:

    request.addHeaderValue("application/json", forName: "Content-Type")