cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Service to authenticate and POST to external system

Highlighted

Re: Service to authenticate and POST to external system

Thank you all! After trying different things what worked for me was grabbing the cookie that I had received during authentication and setting it in my subsequent requests in the headers  as "Cookie" and not "Set-Cookie" (like browsers do), and then converting it to JSON payload, as below:


var json = me.Authenticate();

var cookie = {"Content-Type":"application/json","Cookie": json["_cookies"]};

var headers = JSON.stringify(cookie);

........ etc.......


I also believe this link might have some answers https://support.ptc.com/apps/solution_preview/solution/lang/presolution?lang=en&n=CS245905 but, unfortunately, I cannot see it at the moment, as I don't have support account. Still evaluating Thigworx...

Re: Service to authenticate and POST to external system

Yuriy, here's what the resolution contains for that article, (BTW could you try creating an account on support.ptc.com under ThingWorx if you haven't already)

  • Cookies are sent to a service as a string of key-value pairs that are passed in as part of the HTTP Request headers.
    • They use the format "key1=value1; key2=value2;..."
  • In order to send a cookie to a service using the ContentLoaderFunctions.PostJSON service, a "Cookie" header needs to be defined.

Example Code:

// Define the headers that need to be sent with our service call // The "Cookie" header passes in a semi-colon separated list of cookies (attribute=value pairs) var headers = {"Cookie": "JSESSIONID=AB12397DCFB2342;myCookie=value123"};  var params = { headers: headers /* JSON */, url: "http://myserver.com/Services/MyService" /* STRING */, };  var result = Resources["ContentLoaderFunctions"].PostJSON(params);

Similarly, other headers can be added to the JSON request by adding additional attributes in the JSON object:

// Define all headers to be included with the request var headers = { "Cookie": "JSESSIONID=AB12397DCFB2342;myCookie=value123", "Content-Type": "application/json", "appKey": "a4823bdc48555aff" };  var params = { headers: headers /* JSON */, url: "http://myserver.com/Services/MyService" /* STRING */ };  var result = Resources["ContentLoaderFunctions"].PostJSON(params);
Highlighted

Re: Service to authenticate and POST to external system

Thank you Sushant for all the help!

Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum! ______________________________