Search code examples
xmlrrcurlhttr

httr POST API request but missing xml parameter


I am trying to connect to the XML API for IBM Silverpop using httr, but I am running into a 'missing XML parameter' problem. I tried using the test harness provided by Silverpop to see if the requests I am sending is different in comparison to what I send via httr, but haven't found anything to help me.

Following is the XML body I send when using the test harness which succeeds:

<Envelope><Body>
<Login>
<USERNAME>My Email</USERNAME>
<PASSWORD>My password</PASSWORD>
</Login>
</Body></Envelope>

Using Chrome to inspect the request headers (using CTRL + SHIFT + I):

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:225
Content-Type:application/x-www-form-urlencoded
Host:api3.silverpop.com
Origin:null
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36

Again, using Chrome to inspect response headers:

Connection:Keep-Alive
Content-Type:text/xml;charset=utf-8
Date:Fri, 04 Dec 2015 12:15:34 GMT
Keep-Alive:timeout=15, max=100
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

Using httr, I construct my request as follows:

library(httr)

ibmUrl <- "api3.silverpop.com/XMLAPI"

body1 <- "<Envelope><Body>
<Login>
<USERNAME>My Email</USERNAME>
<PASSWORD>My password</PASSWORD>
</Login>
</Body></Envelope>"

test1 <- POST(url = ibmUrl, body = body1, verbose(), content_type("application/x-www-form-urlencoded"))

Using verbose() shows the following being sent and received:

-> POST /XMLAPI HTTP/1.1
-> Host: api3.silverpop.com
-> User-Agent: libcurl/7.43.0 r-curl/0.9.3 httr/1.0.0
-> Accept-Encoding: gzip, deflate
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: application/x-www-form-urlencoded
-> Content-Length: 132
-> 
>> <Envelope><Body>
>> <Login>
>> <USERNAME>My Email</USERNAME>
>> <PASSWORD>My password</PASSWORD>
>> </Login>
>> </Body></Envelope>

<- HTTP/1.1 200 OK
<- Date: Fri, 04 Dec 2015 13:29:55 GMT
<- Server: Apache-Coyote/1.1
<- Content-Type: text/xml
<- Content-Length: 255

But when I inspect the response from the API using content(), I get the following:

<?xml version="1.0"?>
<Envelope>
  <Body>
    <RESULT>
      <SUCCESS>false</SUCCESS>
    </RESULT>
    <Fault>
      <Request/>
      <FaultCode/>
      <FaultString>Missing 'xml'parameter</FaultString>
      <detail>
        <error>
          <errorid>52</errorid>
          <module/>
          <class>SP.API</class>
          <method/>
        </error>
      </detail>
    </Fault>
  </Body>
</Envelope>

How come?

Thanks in advance.


Solution

  • I think I found the answer - although not quite sure why this works and my original attempt shown in the question did not...

    This was my original code:

    test1 <- POST(url = ibmUrl, body = body1, 
    verbose(), content_type("application/x-www-form-urlencoded"))
    

    All I had to change was the content_type to 'text/xml'.

    test1 <- POST(url = ibmUrl,
            body = body1, verbose(), content_type("text/xml"))
    

    And it works... But would be thankful if anybody could chime in and clarify why this solves the problem...