Skip to main content
1-Visitor
January 9, 2017
Solved

Cookie as header

  • January 9, 2017
  • 6 replies
  • 4261 views

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

Best answer by jamesm1

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.

6 replies

1-Visitor
January 9, 2017

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

magrawal1-VisitorAuthor
1-Visitor
January 9, 2017

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.

1-Visitor
January 9, 2017

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