Scripted REST API POST how to retrieve the response?

lucap_
Mega Sage

Goodmorning,

I've created a scripted REST API with a post function to let a third party create a kind of incident, I whould like to log messages (inbound and outbound) into a custom table so I need to retrieve the response that we send to the third party. I can get my request's body because of I create it with the StreamWriter, but I whould like to get even the header created with the response.setStatus() and response.setContentType() : ex.

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 04 Aug 2015 15:20:44 GMT
Server: ServiceNow
Connection: close
Set-Cookie: BIGipServerpool_<Instance>=880838154.47166.0000; path=/

{"result":{"id":1234,"id1":5678,"name":"user0","name1":"user1"}}

Is there any way to do that?

Best regards

Luca

5 REPLIES 5

Alikutty A
Tera Sage

Hi Luca,



Please add the script in your scripted REST API and you can access all headers send in the request from external system



var header = request.headers;


for (var i in header){


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


}



Try calling your post API and check the system logs once.



Thank You


Please Hit Like, Helpful or Correct depending on the impact of response


Hi Alikutty,


thanks for your reply, but I was looking for response headers not for request headers.


Hi Luca,


While executing the POST message object, capture the response in a variable.


Eg:-


var res = yourPOST-JSONobject.execute;


Now using OOB methods you can get the response headers:-



var resBod = res.getBody();


var resStat = res.getStatusCode();



Thanks,


Subhajit


Hi Subhajit,


thanks for your reply, unfortunatelly I can't use this OOB methos...This is my code:


(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {


  var ris=[];


  var corpo_req= request.body.dataString;


  var ogg= JSON.parse(corpo_req);


  var ins=new REST_insert_incident();


        var dati_resp=ins.crea_inc(ogg.tipo_di_problema,ogg.tipo_di_prodotto,ogg.breve_descr,ogg.impatto,ogg.urgenza);// Data validation and incident creation


  //gs.log("Dimensione->"+dati_resp.length+" problema->"+ogg.tipo_di_problema+" prodotto->"+ogg.tipo_di_prodotto,"incident_rest");


  if(dati_resp.length>2){


  corpo_resp={


  'number':dati_resp[1],


  'sys_id':dati_resp[2]


  };


  ris.push(corpo_resp);


  }


  else {


  corpo_resp={


  'message':dati_resp[1]


  };


  ris.push(corpo_resp);


  }


  response.setStatus(dati_resp[0]);//201 se è stato inserito 400 altimenti


  response.setContentType('application/JSON;charset=UTF-8');


  var writer=response.getStreamWriter();


  writer.writeString(JSON.stringify({risultato:ris}));


  //At the moment I try to build a kind of response, but it's not excactly the response...


  var res_log="Status code :"+dati_resp[0]+" Content-Type :application/JSON;charset=UTF-8"+JSON.stringify({risultato:ris});


  gs.log("Ver output->"+res_log,"API");


})(request, response);