1

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!

4
  • 2
    Is the generated xml payload from your code correct? Per code, the email, firstname, etc.. are set as attributes of your 'subscription' element. If you need 'email','firstname',etc.. as child elements, you should use appendChild() instead of setAttribute() Commented Jul 1, 2013 at 21:01
  • Please go back and put in the < and > and indent the whole thing 4 spaces, so we don't have to guess or read your mind. If it's all one line, leave it on one line and let SO display it in a scrolling code widget. Commented Jul 1, 2013 at 21:13
  • @user1573133 Your comment (without the first sentence) should be an answer. Commented Jul 1, 2013 at 21:15
  • @JimGarrison: Thx. Added as an answer. Commented Jul 2, 2013 at 2:08

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

oh wow, thanks a lot! that's the kind of thing that I was confused about. I'm extremely new to XML and JSON in java so getting the correct payload is tough but this is extremely helpful! Gonna go try it out! I feel like that was the issue though! THANKS!!!
IT WORKED! you're a life saver!!! it's the simple things like that that I love about programming. so subtle, yet so important. thanks again !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.