Search code examples
jsonrestibm-mobilefirstworklight-adapters

POST Adapter HTTP Worklight 6 500 Server Error


I am trying to register a user in my Hybrid Worklight application. For this I am using an external REST API. The API says:

  • Request Method: POST
  • Request URL: https://${domainservice}/service/plan/${planName}/user?key=${key}&tenantId=${tenantId}
  • Request Headers: Content-Type:application/json
  • Request Payload:

    { "uid": "patricia", "firstName": "Patricia", "lastName": "Mayo", "pic": "BASE64_ENCODED_IMAGE" }

Field description:

  • uid (required): user's uid
  • firstName (optional): user's first name
  • lastName (optional): user's last name
  • pic (optional): user's picture encoded as base64 string

So I created a HTTP Worklight adapter:

function RegisterUser(userid) {
    var input = {
        method : 'post',
        path : '/service/plan/App/user',
        returnedContentType : 'plain',
        headers: {'Content-Type' : 'application/json'},         
        parameters: { 
                'key':'e634bc60-0c6eba577258',
                'tenantId': 'd93b921d-a56c-a645924fd548'                    
        },
        body : {
                'contentType' : 'application/json',
                'content' : JSON.stringify({
                    "uid" : userid})    
        }
    };

    return WL.Server.invokeHttp(input);
}

And I get this error:

   { "errors": [
   ],
   "info": [
   ],
   "isSuccessful": true,
   "responseHeaders": {
      "$wsep": "",
      "Connection": "Keep-Alive",
      "Content-Language": "en-US",
      "Content-Type": "text\/html;charset=ISO-8859-1",
      "Date": "Wed, 30 Jul 2014 14:47:27 GMT",
      "Transfer-Encoding": "chunked",
      "X-Backside-Transport": "FAIL FAIL",
      "X-Client-IP": "199.127.32.67",
      "X-Global-Transaction-ID": "48515650",
      "X-Powered-By": "Servlet\/3.0"
   },
   "responseTime": 357,
   "statusCode": 500,
   "statusReason": "Internal Server Error",

I think is very weird that I set up

headers: {'Content-Type' : 'application/json'}

but in the response it looks like

"Content-Type": "text/html;charset=ISO-8859-1"

Also I want to point out some things I have already tried:

  • returnedContentType : 'plain' --> It is set to plain because if I set it to json I would get a JSON parse error

  • body content I also tried

    var payload = "{\'uid\': \'"+userid+"\'}";  
    payload = payload.toString();
    

    and then 'content' : payload

  • Using the RESTClient of the browser everything works fine

  • I tried using http port 80, http port 2080, and also https 443

  • I also tried writting the Host in the headers


Solution

  • I finally found the error. There is a defect in Worklight, the query parameters are removed and put into request body so the REST SERVICE does not receive key & tenantId. This is the final working code solution. Thank you anyways for your quick answers :)

    function RegisterUser(userid) {
        var payload = '{"uid": \"'+userid+'\"}';  
        var input = {
            method : 'post',
            path : '/service/plan/App/user?key=e630-db87-4803-bc45-57725c&tenantId=d9348af-a56c-a645924fd54e',
            returnedContentType : 'application/json',
            headers: {'Content-Type' : 'application/json'},         
            body : {
                        'contentType' : 'application/json',
                        'content' : payload.toString()
            }
        };
    
        return WL.Server.invokeHttp(input);
    }