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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Cookie as header

magrawal
2-Explorer

Cookie as header

Hi,

I need to pass a cookie as header to server in order to get data from that.
For this I am Using following:

var Cookieparams = {

  proxyScheme: undefined /* STRING */,

    headers: {"Cookie":"UFJKKFF838NNBDH38B"} /* JSON */,

  ignoreSSLErrors: true /* BOOLEAN */,

  withCookies: true /* BOOLEAN */,

  url: "url?Parameters" /* STRING */,

};

// result: STRING

var result = Resources["ContentLoaderFunctions"].GetJSON(Cookieparams);

As a result it's displaying "No active sessions with given cookie". I suspect problem caused because of above code.

Please suggest a way to pass cookie as header.

Thanks,

Meenakshi

ACCEPTED SOLUTION

Accepted Solutions
jamesm1
12-Amethyst
(To:nkhose)

This is the correct method; the important difference is that you are using the result of GetCookies as the cookie header. Note that sometimes the cookie result goes into a Cookie header, as in the original post:

i.e:

var params = {

  proxyScheme: undefined /* STRING */,

  //  headers: {"JSESSIONID":"4C211033F290F29A152173A47A26FC78"} /* JSON */,

  ignoreSSLErrors: true /* BOOLEAN */,

  useNTLM: undefined /* BOOLEAN */,

  workstation: undefined /* STRING */,

  useProxy: undefined /* BOOLEAN */,

  withCookies: true /* BOOLEAN */,

  proxyHost: undefined /* STRING */,

  url: "http://localhost:8080/Thingworx/Things" /* STRING */,

  timeout: undefined /* NUMBER */,

  proxyPort: undefined /* INTEGER */,

  password: "admin" /* STRING */,

  domain: "http://localhost:8080/" /* STRING */,

  username: "Administrator" /* STRING */

};

// result: STRING

var cookieResult = Resources["ContentLoaderFunctions"].GetCookies(params);

var Cookieparams = {

  proxyScheme: undefined /* STRING */,

    headers: {"Cookie":cookieResult } /* JSON */,

  ignoreSSLErrors: true /* BOOLEAN */,

  withCookies: true /* BOOLEAN */,

  url: "url?Parameters" /* STRING */,

};

// result: STRING

var result = Resources["ContentLoaderFunctions"].GetJSON(Cookieparams);

This will depend on the format required by the receiving server, but should match whatever works in postman.

View solution in original post

6 REPLIES 6
nkhose
12-Amethyst
(To:magrawal)

var params = {

  proxyScheme: undefined /* STRING */,

  //  headers: {"JSESSIONID":"4C211033F290F29A152173A47A26FC78"} /* JSON */,

  ignoreSSLErrors: true /* BOOLEAN */,

  useNTLM: undefined /* BOOLEAN */,

  workstation: undefined /* STRING */,

  useProxy: undefined /* BOOLEAN */,

  withCookies: true /* BOOLEAN */,

  proxyHost: undefined /* STRING */,

  url: "http://localhost:8080/Thingworx/Things " /* STRING */,

  timeout: undefined /* NUMBER */,

  proxyPort: undefined /* INTEGER */,

  password: "admin" /* STRING */,

  domain: "http://localhost:8080/" /* STRING */,

  username: "Administrator" /* STRING */

};

// result: STRING

var result = Resources["ContentLoaderFunctions"].GetCookies(params);

// result: JSON

//var result = Resources["ContentLoaderFunctions"].GetJSON(params); // Return JSON

You can get cookie first or pass in the cookie using get cookie  then use GetJSON and pass cookie from the result set of  GetCookies to get the data what you need

I can get cookie as well as set cookie see above  or let me know you need any other way to achieve it

magrawal
2-Explorer
(To:nkhose)

Thanks Niesh,

I am doing the exactly as I mentioned. I am able to get cookies from the server but when requesting GETJson with received cookie I am always getting "No active session". Same thing I can perform through POSTMAN.

nkhose
12-Amethyst
(To:magrawal)

I could not encountered  the error you are facing tried with postman as well ,

my whole code

var params = {

  proxyScheme: undefined /* STRING */,

  //  headers: {"JSESSIONID":"4C211033F290F29A152173A47A26FC78"} /* JSON */,

  ignoreSSLErrors: true /* BOOLEAN */,

  useNTLM: undefined /* BOOLEAN */,

  workstation: undefined /* STRING */,

  useProxy: undefined /* BOOLEAN */,

  withCookies: true /* BOOLEAN */,

  proxyHost: undefined /* STRING */,

  url: "http://localhost:8080/Thingworx/Things" /* STRING */,

  timeout: undefined /* NUMBER */,

  proxyPort: undefined /* INTEGER */,

  password: "admin" /* STRING */,

  domain: "http://localhost:8080/" /* STRING */,

  username: "Administrator" /* STRING */

};

// result: STRING

var cookieResult = Resources["ContentLoaderFunctions"].GetCookies(params);

var params = {

  proxyScheme: undefined /* STRING */,

   headers: {"JSESSIONID":cookieResult} /* JSON */,

  ignoreSSLErrors: true /* BOOLEAN */,

  useNTLM: undefined /* BOOLEAN */,

  workstation: undefined /* STRING */,

  useProxy: undefined /* BOOLEAN */,

  withCookies: true /* BOOLEAN */,

  proxyHost: undefined /* STRING */,

  url: "http://localhost:8080/Thingworx/Things" /* STRING */,

  timeout: undefined /* NUMBER */,

  proxyPort: undefined /* INTEGER */,

  password: "admin" /* STRING */,

  domain: "http://localhost:8080/" /* STRING */,

  username: "Administrator" /* STRING */

};

// result: JSON

var result = Resources["ContentLoaderFunctions"].GetJSON(params); // REturn JSON

Output is JOSN

You may try with different username and pass

jamesm1
12-Amethyst
(To:nkhose)

This is the correct method; the important difference is that you are using the result of GetCookies as the cookie header. Note that sometimes the cookie result goes into a Cookie header, as in the original post:

i.e:

var params = {

  proxyScheme: undefined /* STRING */,

  //  headers: {"JSESSIONID":"4C211033F290F29A152173A47A26FC78"} /* JSON */,

  ignoreSSLErrors: true /* BOOLEAN */,

  useNTLM: undefined /* BOOLEAN */,

  workstation: undefined /* STRING */,

  useProxy: undefined /* BOOLEAN */,

  withCookies: true /* BOOLEAN */,

  proxyHost: undefined /* STRING */,

  url: "http://localhost:8080/Thingworx/Things" /* STRING */,

  timeout: undefined /* NUMBER */,

  proxyPort: undefined /* INTEGER */,

  password: "admin" /* STRING */,

  domain: "http://localhost:8080/" /* STRING */,

  username: "Administrator" /* STRING */

};

// result: STRING

var cookieResult = Resources["ContentLoaderFunctions"].GetCookies(params);

var Cookieparams = {

  proxyScheme: undefined /* STRING */,

    headers: {"Cookie":cookieResult } /* JSON */,

  ignoreSSLErrors: true /* BOOLEAN */,

  withCookies: true /* BOOLEAN */,

  url: "url?Parameters" /* STRING */,

};

// result: STRING

var result = Resources["ContentLoaderFunctions"].GetJSON(Cookieparams);

This will depend on the format required by the receiving server, but should match whatever works in postman.

magrawal
2-Explorer
(To:jamesm1)

Thanks James McCuen​,

I did the same as your instructions but still getting "No active session with supplied cookie" on passing received cookie as a header. I am getting cookie from our server. Is there any possibility that Thingworx cookie JSESSIONID is overwriting my  server's cookie which I am sending in header and that's why it is always responding that there is no active session with supplied cookie.

Meenakshi

magrawal
2-Explorer
(To:jamesm1)

Hey James McCuen​,

It's working for me as well now. The problem was due to spaces in headers.
My code was:
headers:{"Cookie":cookieResult} /* JSON */,


and the code that works as expected for me is:

headers: { "Cookie" : cookieResult  } /* JSON */,



Thanks,

Meenakshi

Announcements


Top Tags