Question

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

Was it helpful?

Solution 3

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

OTHER TIPS

I would not be too concerned about the format of the payload in the case where the server has hit an error condition - ideally servers would give us nicely formed error response but that's not always possible. Typically, if the expected response type in case of success is JSON then that's what I specify, and I just have to have enough error handling for cases when JSON parsing fails.

As to why you're getting the 500 error ... best I can suggest is to use a network monitor of some sort to discern the difference between the request issued via Worklight and the request as issued by the Browser's REST client.

You can set this up in Eclipse, Preferences->Run->TCP/IP Monitor ...

You may want to add a Host header. I've seen numerous times where it solved similar issues. E.g. if you're trying to connect to http://www.server.com/a/b/c/d add below header to your headers property

headers: {
    Host: 'www.server.com'
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top