Jennifer
Jennifer

Reputation: 31

How To Post XML via JavaScript to REST API?

I'm trying to POST XML via JavaScript to a REST API.

The Request Data looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<EditGame xmlns="http://blahblahblah.com" >
<playerCount>2</playerCount>
<score>2621440</score>
</EditGame>

How do I define the postString above if my code looks like this below:

xhr.open('POST',URLgameUpdateAction);
xhr.setRequestHeader('Content-type','application/x-www.form-urlencoded');
xhr.send(**postString**);

Hope this makes sense.

Upvotes: 3

Views: 6008

Answers (1)

mhitza
mhitza

Reputation: 5715

You can pass the XML as a simple string.

xhr.open('POST',URLgameUpdateAction);
xhr.setRequestHeader('Content-type','application/x-www.form-urlencoded');
xhr.send("\
  <?xml version='1.0' encoding='UTF-8' standalone='yes'?>\
  <EditGame xmlns='http://blahblahblah.com'>\
  <playerCount>2</playerCount>\
  <score>2621440</score>\
  </EditGame>\
");

Upvotes: 1

Related Questions