function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dfcdfc 

Passing an HTTP Parameter to a Non-SOAP XML API

I am having trouble writing an Apex class that integrates to a non-SOAP based XML-over-HTTPS web service.  The problem is that the API requires an HTTP request parameter (not a header) to be set.  Here is their documentation on this:

 

 

"To implement the XML Data Integration Toolkit in an application, user must post the request to the Data Integration server with the XML request in a specific parameter.

 

 

I naively hoped that the following code would work in an Apex class to access this API (since the HttpRequest Apex class does not have a setParameter() or setField() method -- like the equivalent Java or C# request classes do -- just the setHeader() method):

 

    public static String getLookupResponse() {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://toolkit.dnb.com/access/scripts/broker.asp');
        req.setMethod('POST');
        req.setTimeout(60000);
        req.setBody(m_lookupTemplate);
        HttpResponse res = h.send(req);
        return res.getBody();
    }

where m_lookupTemplate is like the following (which works in their test harness HTML page):

 

<?xml version='1.0' encoding='UTF-8' ?>
<DGX>
  <SIGNONMSGSRQV1>
    <SONRQ>
      <DTCLIENT>2009-04-04 10:49:28</DTCLIENT>
      <USERID>craigmiled</USERID>
      <USERPASS>XXXXXXXXX</USERPASS>
      <LANGUAGE>EN</LANGUAGE>
      <FI>
        <ORG>DUN and BRADSTREET</ORG>
      </FI>
      <APPID>XML dnbToolkit</APPID>
      <APPVER>0010</APPVER>
    </SONRQ>
  </SIGNONMSGSRQV1>
  <CREDITMSGSRQV2>
    <LOOKUPTRNRQ>
      <TRNUID>242DF2924CC46</TRNUID>
      <LOOKUPRQ>
        <CTRY_CD>US</CTRY_CD>
        <PRIM_NME>Caterpillar</PRIM_NME>
        <TLCM_NBR></TLCM_NBR>
        <ADR_LINE></ADR_LINE>
        <NON_POST_TOWN></NON_POST_TOWN>
        <POST_TOWN></POST_TOWN>
        <PRIM_GEO_AREA>IL</PRIM_GEO_AREA>
        <POST_CODE></POST_CODE>
        <DUNS_NBR></DUNS_NBR>
        <REGN_NBR></REGN_NBR>
        <FILE_ID></FILE_ID>
        <RTN_MAX></RTN_MAX>
        <PURC_REAS_CD></PURC_REAS_CD>
        <MTCH_TYPE></MTCH_TYPE>
      </LOOKUPRQ>
    </LOOKUPTRNRQ>
  </CREDITMSGSRQV2>
</DGX>

 

This does not work as the server returns an "invalid request" result.

 

I tried setHeader('request',m_lookupTemplate) on HttpRequest instead of setBody().  No luck.  The basic problem is that HttpRequest does not allow you to programatically set request parameters.  Any ideas?

 

Thanks!

 

Dave

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
Its weird that a data api is based on html forms, but given that, you'll need to build your HTTP request to match the format used by HTML forms. (in this case request=<your xml...., you may need to do some work to encode certain characeters). You can try and decode the spec and/or look at the raw http request of a working request to the service you're trying to call.
Message Edited by SimonF on 05-28-2009 04:50 PM

All Answers

SuperfellSuperfell
Assuming by request parameters you mean an application/x-www-form-urlencoded HTML form POST, then you'll need to build the specific html form encoded body yourself.
ryanhcaryanhca

I don't have a for-sure answer for you - it looks like you're on the right track. I just did a google search for "HTTP POST parameters" and came up to this page:

http://developers.sun.com/mobility/midp/ttips/HTTPPost/

 

It does show that the "body" of the request would be the URL-encoded name/value pairs of the request parameters. Two things occur to me:

1. I don't see the "request=" in the first part of the body

2. is the XML returned from the template URL-encoded?

 

 

Lastly, I don't know of a good way to debug the final output, but perhaps send the HttpRequest.getBody() results to the debug log to see if it's coming back the way you want. You might also try using the static results of that call in another call to the service using Java to see if the service will accept your data...

 


Good luck. 

 

dfcdfc
Thanks for the rapid response, Simon! So, I need to programatically format my payload as a form input parameter named 'request'. I am currently calling: req.setBody(); Are you suggesting wrapping the XML Payload with and tags (etc), or something else? Remember, I am doing this programatically. There is no visual page I am constructing and submitting. Thanks! Dave
SuperfellSuperfell
Its weird that a data api is based on html forms, but given that, you'll need to build your HTTP request to match the format used by HTML forms. (in this case request=<your xml...., you may need to do some work to encode certain characeters). You can try and decode the spec and/or look at the raw http request of a working request to the service you're trying to call.
Message Edited by SimonF on 05-28-2009 04:50 PM
This was selected as the best answer
dfcdfc

Thanks Simon and Ryan!  I'll give that a shot and let you know.

 

Dave

 

dfcdfc

You guys are geniuses!  I replaced:

 

req.setBody(XML Mess)

 

with:

 

req.setBody('request=' + URL Encoded XML Mess)

 

and it worked.

 

Dave