cancel
Showing results for 
Search instead for 
Did you mean: 

Corresponding statement to JSON.stringify for XML outputs in XSJS

shyam_uthaman
Participant
0 Kudos

Hi All,

My XSJS fails to get data from an external server due to the line 08 below.

My input is an XML and the output returned is also XML and not JSON.

I just can't seem to find the corresponding XML counterpart for JSON.stringify. Let me know please if you are aware of the answer.


req.contentType = "text/xml";

     req.setBody(data);

     client.request(req, dest);

     var response = client.getResponse();

    

  var resBody = response.body.asString();

$.response.setBody(JSON.stringify({ //This might be the problem

    "body" : resBody;

Thanks,

Shyam

Accepted Solutions (0)

Answers (3)

Answers (3)

shyam_uthaman
Participant
0 Kudos

Any thoughts?

shyam_uthaman
Participant
0 Kudos

Hi,

In the code before the statements I have written -


var dest = $.net.http.readDestination("Path","AddrService");

var client = new $.net.http.Client();

var req = new $.net.http.Request($.net.http.GET, "service/1services/addr");

var data = '<?xml version="1.0" encoding = "UTF-8"?>'

               +<inputs...>

that is why I was using the above variable req for req.contentType which may be wrong due to my inexperience with javascript.

Anyways, I modified the code in try{} block as you suggested to


response.contentType = "text/xml";

     req.setBody(data);

     client.request(req, dest);   

  var resBody = response.body.asString();

     $.response.status = $.net.http.OK;

  $.response.setBody(resBody);

which threw an error that "response is not defined"

Now I added another line as line 01 below to define response ( I hope that is correct)


var response = client.getResponse();

     response.contentType = "text/xml";

     req.setBody(data);

     client.request(req, dest);   

  var resBody = response.body.asString();

     $.response.status = $.net.http.OK;

  $.response.setBody(resBody);

The current error says

Does this have something to do with the order of this code?

Thanks,

Shyam

0 Kudos

Hi,

can you attach the file if possible?

shyam_uthaman
Participant
0 Kudos

Hi,

Attached here is the code and the XML

Thanks for your time.

Regards,

Shyam

0 Kudos

Try the attached code.

shyam_uthaman
Participant
0 Kudos

Hi,

New error:

Shyam

0 Kudos

Hi,

  You don't need to stringify XML. You have already got it as string


var resBody = response.body.asString(); 

  1. $.response.status = $.net.http.OK; 
  2. $.response.contentType = "text/html"
  3. $.response.setBody(resBody );
shyam_uthaman
Participant
0 Kudos

Hi,

Thanks for the reply but I still face the same issue.

I modified the code to


req.contentType = "text/html";

     req.setBody(data);

     client.request(req, dest);

     var response = client.getResponse();

  var resBody = response.body.asString();

     $.response.status = $.net.http.OK;

  $.response.setBody(resBody);

Also, the content type should be text/xml and not text/html ..right?

Let me know please.

Thanks,

Shyam

0 Kudos

you have to set content type of response.


 


$.response.contentType = "text/xml"

Sorry, it's NOT text/html.