0

I was making a POST request in the following manner, using URL params (which worked):

var PAYLOAD = `
  <myxmlcontent>
    <attribute name="id">1</attribute>
    <attribute name="FullName">Joe Bloggs</attribute>
  </myxmlcontent>
`

var URL = 'http://www.somewhere.com/integration?apiKey=company&apiToken=123&payload=' + PAYLOAD;

client.request({
  url: URL,
  type: 'POST',
  contentType: 'application/xml'
}).then(
  function(data) {
    console.log(data);
  }
);

But I wish to put the payload data into the request body.

Is this the correct way to do it? I am not sure, but my attempt has proved unsuccessful so far:

var PAYLOAD = `
  <myxmlcontent>
    <attribute name="id">1</attribute>
    <attribute name="FullName">Joe Bloggs</attribute>
  </myxmlcontent>
`

client.request({
  url: 'http://www.somewhere.com/integration',
  type: 'POST',
  contentType: 'application/xml',
  headers: {
    apiKey: 'company',
    apiToken: '123'
  },
  dataType: 'xml',
  data: 'data=' + JSON.stringify(PAYLOAD)
}).then(
  function(data) {
    console.log(data);
  }
);

I am currently building a client-side Zendesk app.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Tom
  • 9
  • 4
  • yeah, that's mostly right, you just didn't encode the param value. and your content type doesn't match the content you are sending. A param string may contain an xml value, but it's still a param string, not xml. – Kevin B May 08 '17 at 20:19
  • Hi Kevin, just to say I appreciate your help – Tom May 08 '17 at 20:27
  • If I have understood correctly, I have made the following changes to the code as a result. Before the request: **PAYLOAD = encodeURIComponent(PAYLOAD)** In the request body: **dataType: 'text',** **data: 'data=' + JSON.stringify(PAYLOAD)**. Unfortunately, I am still getting a 500 response. – Tom May 08 '17 at 20:36
  • 1
    what does the server expect you to give it? – Kevin B May 08 '17 at 20:37
  • I am reading the associated docs with the API. _The payload request parameter in inbound API is XML._ I am not sure how noteworthy this is: _If the integration is on web service mode, messages between **** and the client application is over HTTP/HTTPS. All the APIs take input in key value pair style (not in REST style) and responds to the client in XML format. Authentication is through exchange of a login id/password combination maintained in **** as well as in customer application._ – Tom May 08 '17 at 20:44
  • Sounds like your data needs to use the default content type. simply don't set one. – Kevin B May 08 '17 at 20:46

2 Answers2

1

First you have to make sure the endpoint accepts data via POST, otherwise it will fail even if you are seding your data correctly, secondly, if you want to send data as an url-encoded form, you need to change the contentType to application/x-www-form-urlencoded and send the body as either an url-encoded string or by using the FormData object (if it's available in your framework), e.g.:

var myData = new FormData();
myData.append("payload", encodeURI(PAYLOAD));

client.request({
  url: 'http://www.somewhere.com/integration',
  type: 'POST',
  contentType: 'application/x-www-form-urlencoded',
  headers: {
    apiKey: 'company',
    apiToken: '123'
  },
  dataType: 'xml',
  data: myData
}).then(
  function(data) {
    console.log(data);
  }
);

Don't forget to also encode the content of payload. In case your endpoint only accepts xml-encoded strings then you'd have to send the string as-is, just make sure to specify the correct contentType, in which case would be application/xml or text/xml.

arielnmz
  • 8,354
  • 9
  • 38
  • 66
  • Thank you arielnmz for helping me understand more about this. I will see if I have any success. Thank you for taking the time to provide guidance to others arielnmz and @Kevin_B – Tom May 08 '17 at 21:09
0

SOLVED. This is what I had to do (thank you):

var PAYLOAD = `
  <myxmlcontent>
    <attribute name="id">1</attribute>
    <attribute name="FullName">Joe Bloggs</attribute>
  </myxmlcontent>
`

var URL = 'http://www.somewhere.com/integration';

client.request({
  url: URL,
  type: 'POST',
  contentType: 'application/x-www-form-urlencoded',
  dataType: 'xml',
  data: {
    apiKey: 'company',
    apiToken: '123',
    payload: PAYLOAD
  }
}).then(
  function(data) {
    console.log(data);
  }
);

Helpful article: How are parameters sent in an HTTP POST request?

Community
  • 1
  • 1
Tom
  • 9
  • 4