Rocky Racoon
Rocky Racoon

Reputation: 75

How to Send an XML Data Stream via a HTTP Post Request

So I need to send an XML data stream via a http post request. I'm finishing my first app and trying to add an sms sending capability to it. I need to send an xml file like this:

<sms>
<user> <username>Leeroy</username> <password>Jenkins</password>
</user> <source>000</source>
<destinations>
<phone id="external id1">5xxxxxxxx</phone> <phone id="external id2">5xxxxxxxx</phone> <phone>5xxxxxxxx</phone>
<phone id="">5xxxxxxxx</phone>
</destinations>
<message>This is a message</message>
<timing>30/03/14 10:10</timing>
<response>0</response>
</sms>

to a http address: https://www.blablasms.cm/api

I'd like to send it as part of a click event, like this for example:

$('input').click(function () {
...
...
...
// make an http post request...

How can it be done? Can I do it with an ajax request? What would be a valid ajax request for that kind of use? Can I send the xml data as part of the url?

Thanks!

Upvotes: 1

Views: 1586

Answers (1)

Kostas
Kostas

Reputation: 1903

var pathToPost = 'https://requestb.in/17x6jwi1'; // example path

$.ajax({
  method: 'POST',
  url: pathToPost,
  data: {
    user: {
      username: document.getElementsByTagName('username')[0].textContent,
      password: document.getElementsByTagName('password')[0].textContent,
    },
    source: document.getElementsByTagName('source')[0].textContent,
    destinations: {
      phone1: document.getElementById('external_id1').textContent,
      phone2: document.getElementById('external_id2').textContent
    },
    message: document.getElementsByTagName('message')[0].textContent,
    timing: document.getElementsByTagName('timing')[0].textContent,
    response: document.getElementsByTagName('response')[0].textContent
  }
}).then(function(response) {
  // handle response
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<sms>
  <user>
    <username>Leeroy</username>
    <password>Jenkins</password>
  </user>
  <source>000</source>
  <destinations>
    <phone id="external_id1">5xxxxxxxx</phone>
    <phone id="external_id2">5xxxxxxxx</phone>
  </destinations>
  <message>This is a message</message>
  <timing>30/03/14 10:10</timing>
  <response>0</response>
</sms>

Here you are, tested and it works like a charm!

Upvotes: 1

Related Questions