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
savvyboardersavvyboarder 

HTTPRequest errors

Hi all-

 

I'm curious if anybody can help me out here.  I'm trying to integrate an exact target xml api, and I'm getting a "Bad Request" when making this call.  However, the input string works fine if you post in a browser.  Is there something I'm missing?  The same code works fine for a different call to the exact target api.

 


 

 

public void addSubscriber() {

// add subscriber to list
// Get the XML document from the external server
Http http = new Http();
HttpRequest req = new HttpRequest();

String inputString = 'https://api.dc1.exacttarget.com/integrate.aspx?qf=xml&xml=<exacttarget><authorization><username>myusername</username><password>mypassword</password></authorization><system><system_name>subscriber</system_name><action>add</action><search_type>listid</search_type><search_value>371041</search_value><search_value2></search_value2><values><Email__Address>webmaster@extraspace.com</Email__Address><status>active</status><Full_Name>TESTING 123</Full_Name></values><update>true</update></system></exacttarget>';


req.setEndpoint(inputString);
req.setMethod('GET');


HttpResponse res = http.send(req);

// Log the XML content
System.debug(res.getBody());

// Generate the HTTP response as an XML stream
XmlStreamReader reader = res.getXmlStreamReader();

// Read through the XML
while(reader.hasNext()) {
System.debug('Event Type:' + reader.getEventType());
if (reader.getEventType() == XmlTag.START_ELEMENT) {
System.debug(reader.getLocalName());
}
reader.next();
}

}

 

 

Cheers, 

 

BW

 

Best Answer chosen by Admin (Salesforce Developers) 
savvyboardersavvyboarder
That does it!  Thank you! 

All Answers

aalbertaalbert

Have you added the Remote Site? Go to Setup->Administration Setup->Security Controls->Remote Site Settings

Detailed in this document too. 

savvyboardersavvyboarder
Yes, I have.
savvyboardersavvyboarder
Hey aalbert, I'm wondering if there are any code samples for HTTP POSTs?
aalbertaalbert

Yes, you can find an example in the Force.com for Amazon Web Services application.

 

Here is a snippet:

HttpRequest req = new HttpRequest(); req.setEndpoint('https://s3.amazonaws.com/soap'); req.setMethod('POST'); System.debug('before soap body: ' + soapBody); System.debug('after soap body: ' + soapBody); req.setBody(soapBody); req.setHeader('Content-Type', 'application/soap+xml' ); req.setHeader('SOAPAction',''); System.debug('content length: ' + string.valueof(soapBody.trim().length())); req.setHeader('Content-Length',string.valueof(soapBody.trim().length())); try { Http http = new Http(); System.debug('making S3 request...: ' + req); response = http.send(req);

 


 

cheenathcheenath

You will have to encode the endpoint, because you are using speical char in the url.

 

Your endpoint should look something like this:

 

https://api.dc1.exacttarget.com/integrate.aspx?qf=xml&xml=%3Cexacttarget%3E%3Cauthorization%3E%3Cusername%3Emyusername%3C/username%3E%3Cpassword%3Emypassword%3C/password%3E%3C/authorization%3E%3Csystem%3E%3Csystem_name%3Esubscriber%3C/system_name%3E%3Caction%3Eadd%3C/action%3E%3Csearch_type%3Elistid%3C/search_type%3E%3Csearch_value%3E371041%3C/search_value%3E%3Csearch_value2%3E%3C/search_value2%3E%3Cvalues%3E%3CEmail__Address%3Ewebmaster@extraspace.com%3C/Email__Address%3E%3Cstatus%3Eactive%3C/status%3E%3CFull_Name%3ETESTING%20123%3C/Full_Name%3E%3C/values%3E%3Cupdate%3Etrue%3C/update%3E%3C/system%3E%3C/exacttarget%3E

 

 

 

savvyboardersavvyboarder

Hi cheenath-

 

When i try to encode the URL i get this error.  I haven't been able to find anything in the help files about this error.  Any clue what I'm doing wrong? 

 

System.CalloutException: no protocol: https%3A%2F%2Fapi.dc1.exacttarget.com%2Fintegrate.aspx%3Fqf%3Dxml%26xml%3D%3Cexacttarget%3E%3Cauthorization%3E%3Cusername%3Emyusername%3C%2Fusername%3E%3Cpassword%3Emypassword%21%3C%2Fpassword%3E%3C%2Fauthorization%3E%3Csystem%3E%3Csystem_name%3Esubscriber%3C%2Fsystem_name%3E%3Caction%3Eadd%3C%2Faction%3E%3Csearch_type%3Elistid%3C%2Fsearch_type%3E%3Csearch_value%3E371041%3C%2Fsearch_value%3E%3Csearch_value2%3E%3C%2Fsearch_value2%3E%3Cvalues%3E%3CEmail__Address%3Ewebmaster%40extraspace.com%3C%2FEmail__Address%3E%3Cstatus%3Eactive%3C%2Fstatus%3E%3CFull_Name%3ETESTING+123%3C%2FFull_Name%3E%3C%2Fvalues%3E%3Cupdate%3Etrue%3C%2Fupdate%3E%3C%2Fsystem%3E%3C%2Fexacttarget%3E

 

 

cheenathcheenath

You dont have to encode the protocol and machine part of your url:

 

https://api.dc1.exacttarget.com/integrate.aspx?

 

Only things after that need to encoded.

 

 

savvyboardersavvyboarder

Great, thanks!  That worked for the GET, but I guess I'll need to make it a POST in order to avoid the URL being truncated.  Can you tell me what I'm doing wrong?  It's failing on the http.send with this error. 

 

System.CalloutException: Read timed out

 

 

public void addSubscriber() { // add subscriber to list // Get the XML document from the external server Http http = new Http(); HttpRequest req = new HttpRequest(); String endPointString = 'https://api.dc1.exacttarget.com/integrate.aspx?qf=xml&xml='; String strXML = '<?xml version="1.0"?><exacttarget><authorization><username>myusername</username><password>mypassword</password></authorization><system><system_name>subscriber</system_name><action>add</action><search_type>listid</search_type><search_value>371041</search_value><search_value2></search_value2><values><Email__Address>webmaster@extraspace.com</Email__Address><status>active</status><Full_Name>TESTING 123</Full_Name></values><update>true</update></system></exacttarget>'; endPointString += EncodingUtil.urlEncode(strXML, 'UTF-8'); System.debug(endPointString); req.setEndpoint(endPointString); req.setMethod('POST'); req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); req.setHeader('Content-Length', String.valueOf(endPointString.trim().length())); req.setHeader('Connection', 'close'); HttpResponse res = http.send(req); // Log the XML content System.debug(res.getBody()); // Generate the HTTP response as an XML stream XmlStreamReader reader = res.getXmlStreamReader(); // Read through the XML while(reader.hasNext()) { System.debug('Event Type:' + reader.getEventType()); if (reader.getEventType() == XmlTag.START_ELEMENT) { System.debug(reader.getLocalName()); } reader.next(); } }

 

 

 

cheenathcheenath

If it is a post then you should send the XML in the HTTP body instead of encoding it into the URL.

 

To do with you have to call req.setBody(...).

 

 

 

 

 

savvyboardersavvyboarder
That does it!  Thank you! 
This was selected as the best answer