Nurdin
Nurdin

Reputation: 23893

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": [
   ]
}

Upvotes: 0

Views: 2296

Answers (3)

Nurdin
Nurdin

Reputation: 23893

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);
}

Upvotes: 2

Idan Adar
Idan Adar

Reputation: 44516

Do you mean how to stringify the response? Worklight does this for you.

Upvotes: 0

Anton
Anton

Reputation: 3166

var jsonString = JSON.stringify(jsonObj);

other way around

var jsonObj = JSON.parse(jsonString);

Note Idan's answer above. If you're talking about parsing response from a backend - WL does this for you automatically.

Upvotes: 1

Related Questions