Does httppost support xml body?

I am trying to use httppost to send an XML string in the body. However, I am receiving an incorrect body when using PostCatcher utility. Here is my code:

def params = [
            uri: "http://postcatcher.in/catchers/5585ba9c62345703000004f",
            contentType: "application/x-www-form-urlencoded",
            requestContentType:"application/x-www-form-urlencoded", 
            headers: [
            	"x-ms-date":"06=15-2015",
                "x-ms-version":"some version",
                "Authorization":"some value"
            ],           
            body: "<tag1><tag2>More text</tag2></tag1>"    
        ]
               
        try {
            httpPost(params) { resp -> 
               log.debug resp.data;
               //do stuff here
            }
       }
       catch(e){
           //handle exception here.
       }

I get the following body on Postcatcher server. This resembles JSON format. How can I force it to be plain xml. I have tried setting content type to application/xml without success.

{

“<tag1><tag2>More text</tag2></tag1>”: “”

}

How can I force the body to be plain xml?

Try changing

To this, it might help

body: """<tag1><tag2>More text</tag2></tag1>"""

Thanks for your suggestion. However, same body received.

Why not just parse it as json, using httppostjson and then get the key and use that as XML?

I am sending body to a third party server that expects XML.I have no influence over this third party server.

OK, what is the expected XML format? Does it need to validate before sending? What you are sending isn’t XML. Have you tried text/XML?

What is the response you are getting?

Thanks for your response.
The XML format is
<QueueMessage><MessageText>My message goes here</MessageText></QueueMessage>
The only validation would be ensuring that the text is in format above.

The received body remains the same when I change contentType to text/XML
The received body is empty when I change both contentType and requestContentType to text/XML

So, you haven’t posted the response.

Have you traced the post in something that validates the transaction?

Afraid you haven’t given us really anything to help troubleshoot the issue.

XML can be sent to a POST no problem. However, if the endpoint is looking for something specific, you need to know what that is.

I get a response 201 status code which throws an exception. “groovyx.net.http.ResponseParseException: Created”. when I use text/xml for contentType. I get 201 success when I use x-www-form-urlencoded for contentType

The message format is
<QueueMessage><MessageText>Encoded base 64 message goes here</MessageText></QueueMessage>
The third party server parses the line above and actually retrieves some data. However, the data that it retrieves is wrong.because of the improperly formatted body.

Using Fiddler, below is a POST that works fine at the third party server. This post was done by .NET client. Notice the body of the XML.

POST https://mythirdpartyserver.com HTTP/1.1
x-ms-date: Sat, 20 Jun 2015 19:07:15 GMT
x-ms-version: 2014-02-14
Authorization: "some key"
Host: "some host"
Content-Length: 72
Expect: 100-continue
Connection: Keep-Alive

<QueueMessage><MessageText>SGVsbG8gV29ybGQ=</MessageText></QueueMessage>

I cannot get the same thing to work in Groovy so I use the test/debug tool at PostCatcher.and I send my POST there. This is what I see for my POST.

“content-length”: “35”,
“total-route-time”: “0”,
“x-request-start”: “1434914009420”,
“connect-time”: “0”,
“via”: “1.1 vegur”,
“x-forwarded-port”: “80”,
“x-forwarded-proto”: “http”,
“x-forwarded-for”: “54.82.41.209”,
“x-request-id”: “2aa1a14d-773c-43fe-9640-ae3f0d1e0eeb”,
“content-type”: “application/x-www-form-urlencoded”,
“x-ms-date”: “06=15-2015”,
“x-ms-version”: “some version”,
“authorization”: “some value”,
“accept”: “text/xml”,
“connection”: “close”,
“host”: “postcatcher.in”

“<QueueMessage><MessageText>SGVsbG8gV29ybGQ=</MessageText></QueueMessage>”: “”

I used runscope.com and the raws looks fine posting from groovy. Perhaps it’s PostCatcher that’s displaying it incorrectly?

I looked at runscope and I do not see a functionality where runscope act as a server. That is, the request is at runscope. In my case, I want runscope to be accepting my requests.

Below is what I understand of the architecture between runscope and my groovy code
Runscope POST ----> Groovy Code

But I am interested in PostCatcher architecture below
Groovy Code POST —> Runcode server

[quote=“genii90, post:9, topic:18164”]
"<QueueMessage><MessageText>SGVsbG8gV29ybGQ=</MessageText></QueueMessage>": “”
[/quote]The XML has extra quotes around it, because you are sending using the wrong request content type - it’s not a form post, it’s a raw xml string.

Omit requestContentType completely, and set contentType to application/xml

The below code worked on my third party server. However PostCatcher did not work as expected because it displayed a blank body.

def params = [
            uri: "http://postcatcher.in/catchers/5585ba9c62345703000004f",
            contentType: "application/xml",
            requestContentType:"application/xml", 
            headers: [
            	"x-ms-date":"06=15-2015",
                "x-ms-version":"some version",
                "Authorization":"some value"
            ],           
            body: "&lt;tag1&gt;&lt;tag2&gt;More text&lt;/tag2&gt;&lt;/tag1&gt;"    
        ]
               
        try {
            httpPost(params) { resp -&gt; 
               log.debug resp.data;
               //do stuff here
            }
       }
       catch(e){
           //handle exception here.
       }

Thank you all.

You shouldn’t need to urlencode those brackets, but if it’s working run with it :slight_smile:

You could try

body: URLEncoder.encode("<tag1><tag2>More text<tag2><tag1>")

to reduce user entry errors :smile:

Sorry. My copy paste functionality did not work as I intended. This is what I really had

def params = [
            uri: "http://postcatcher.in/catchers/5585ba9c62345703000004f",
            contentType: "application/x-www-form-urlencoded",
            requestContentType:"application/x-www-form-urlencoded", 
            headers: [
            	"x-ms-date":"06=15-2015",
                "x-ms-version":"some version",
                "Authorization":"some value"
            ],           
            body: "<tag1><tag2>More text</tag2></tag1>"    
        ]
               
        try {
            httpPost(params) { resp -> 
               log.debug resp.data;
               //do stuff here
            }
       }
       catch(e){
           //handle exception here.
       }