Search code examples
ibm-mobilefirstworklight-adapters

IBM Worklight 6.1 - How to stringify JSON in worklight adapter?


I'm using IBM Worklight for my mobile app project. My problem is, how to stringify JSON in worklight adapter?

Username-impl.js

 function getUsername(userAlias) {
    path = "rest-rib/service/Login/login_username?userAlias=" + userAlias + "&locale=en";
    
    var input = {
        method : 'post',
        returnedContentType : 'json',
        path : path
    };
    
    
    return WL.Server.invokeHttp(input);
}

I got this error when invoke the adapter.

{
   "errors": [
      "Runtime: Failed to parse JSON string\nError 415: Unsupported Media Type"
   ],
   "info": [
   ],
   "isSuccessful": false,
   "warnings": [
   ]
}

Solution

  • I got the answer

    function getUsername(userAlias) {
            WL.Logger.debug("Entering ContactRESTService1.getUsername()");
            path = '/rest-rib/service/Login/login_username';
    
             var input = {
                          method : 'post',
                          returnedContentType : 'json',
                          path : path,
                          body:{
                          contentType:'application/json; charset=UTF-8',
                          content:
                              JSON.stringify({
                                  "userAlias":userAlias,
                                  "locale":"en"
                              })
                          }
             };
             WL.Logger.debug("Exiting ContactRESTService1.insertContact()");
    
             return WL.Server.invokeHttp(input);
    }