Scripted REST APIs

aswinsiddaling2
Kilo Guru

Hi,

I have created a scripted REST API where I am fetching the HTTP headers by using the below function

request.getHeader('header name').

But I want to fetch all the headers that we receive in the request. I tried to use request.headers as suggested in docs but it gives me [object Object]

Any suggestions?

1 ACCEPTED SOLUTION

Please try this, You should convert the Json to string



var header = request.headers;


var json = new JSON();


var headerStr = json.encode(header);


gs.log(headerStr);



Thanks


PS: Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

10 REPLIES 10

ananthagowraram
Mega Expert

Hi Aswin,



You can get all the header values using response object.Try below code



var response = res.execute(); // res is restmessage object


var error = response.haveError();


var responseBody = response.getBody();


then parse the values using "JSON.parse(responseBody);"



Regards


Anantha Gowraram


Are you saying I will get the http headers by parsing response body?



On Wed, 1 Mar 2017 at 9:44 PM, ananthagowraram <


Alikutty A
Tera Sage

Hi Aswin,



request.headers returns a json string of header values



You can try these lines to get individual header values



var headers = request.headers;
var acceptHeader = headers.Accept;




OR Loop



var header = request.headers;


for (var i in header){


  gs.log(i + " - " + header[i],"Result");


}




where i = header label and header[i] is its value




Thanks


PS: Hit like, Helpful or Correct depending on the impact of the response


Headers.accept will give me the value for http header content-type and not


all headers.



For loop is going to run for number of headers available? Is there no way


to get all the headers without a loop?



On Wed, 1 Mar 2017 at 10:07 PM, alikuttyka <