user2529603
user2529603

Reputation: 27

Sending XML payloads with HTTP POST request using HttpClient--Java

So I've been doing a lot of looking on both stackoverflow and google in general to try and answer my following question, but I have been unable to find anything that can help me to get this work 100%. I'm pretty sure I have everything except a SMALL error correct, but obviously you guys probably have suggestions anyway, so go for it!

And, here we go: I've been using HTTPClient to test an API on a few different environments, and I got HTTPPost methods to accept JSON payloads but now I'm trying to send payloads using XML and I'm running into some issues. It seems that the XML string that I'm creating (in the code below) is correct... so I'm stumped as to why this isn't working. ALSO: got most of the DOM code from the internet (to build up the XML payload) so feel free to bring that into question as well...

My code is the following:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Document doc = docBuilder.newDocument();
Element subscription = doc.createElement("subscription");
doc.appendChild(subscription);

subscription.setAttribute("email", "[email protected]");
etc....
etc....
etc....
etc....

DOMSource domSource = new DomSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);

String XMLpayload = writer.toString();

[name of my HttpRequest].setEntity(new StringEntity(XMLpayload));
[name of my HttpResponse] = client.execute(request);

now... I'm looking to achieve the payload seen BELOW:

<subscription>
    <email>[email protected]</email>
    <firstName>Patricia</firstName>
    <lastName>Miller</lastName>
    <title>Ms.</title>
    <gender>Female</gender>
</subscription>

When I print out the payload that I am sending currently it looks like the following:

?xml version="1.0" encoding="UTF-8" standalone="no"? subscription email="[email protected]" firstName="Patricia" gender="Female" lastName="Miller" title="Ms."/

(NOTE: I REMOVED THE < AND > BRACKETS. THEY APPEAR WHERE THEY SHOULD!)

But, I'm receiving a 400 error. Any ideas here? I know I have the proper headers, the URL is correct, etc. It's absolutely something with what I'm doing with the payload. Any thoughts would be greatly appreciated!!

Best!

Upvotes: 1

Views: 9788

Answers (1)

user1573133
user1573133

Reputation: 964

In your expected payload, 'email','firstname',etc.. are child elements of Subscription element. As per the code, they are added as attributes of your 'subscription' element. If you need 'email','firstname',etc.. as child elements, you should use appendChild() instead of setAttribute().

Element email = doc.createElement("email");
email.appendChild(document.createTextNode("[email protected]"));
subscription.appendChild(email);

Upvotes: 3

Related Questions