Q:
Apex:Id from SOAP request
I'm new in Salesforce Apex and trying to get some data from a SOAP API. My requirement is to get the Id of the Contact where emailId is equal to x@xyz.com.
I'm writing the following code:
HTTP_Headers_Names__c h1 = new HTTP_Headers_Names__c(name='SOAPAction');
h1.value = 'getContactbyEmail';
System.debug('Header Name: ' + h1);
HttpRequest req = new HttpRequest();
req.setHeader('SOAPAction', h1.value);
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/m/47.0/00Dj0000000sTJN');
req.setMethod('POST');
Blob headerValue = Blob.valueOf(h1.value);
req.setHeader('Authorization', 'OAuth 00Dj0000000sTJN!AR_Rcv!AR_Rcv');
req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
req.setHeader('Content-Length', String.valueOf(headerValue.size()));
req.setBody('
x47002015-05-12T11:57:06.883Zxxxxxxxxxxxxxxxxx');
HTTP http = new HTTP();
HTTPResponse res = http.send(req);
System.debug('Response: ' + res.toString());
System.debug('Body: ' + res.getBody());
System.debug('headers: ' + res.getAllHeaders());
System.debug('status: ' + res.getStatus());
And the response is:
DEBUG: Response: 2015-05-13T17:48:27.000ZxxxxxxGAUSxx
DEBUG: Body: [Ljava.lang.String;@7b05f6a6
DEBUG: headers: [Lorg.apache.axis2.databinding.utils.ConverterImpl$JsStringToStringConverter@32a1ca5c, W/Lcom/paypal/api/profiles/profile;]
DEBUG: status: BAD_REQUEST
Can someone suggest me how can I read the values of the response body and header?
Also I am not sure how to set the authorization header. Any help will be really appreciated.
A:
You should use SOAP UI or SoapUI for this kind of stuff. It's an excellent tool to learn how to work with Soap servers and webservices. As for your question, I suggest you read more about header names on wiki before trying to use them (and read more about XML in general). In short you'll need to do
req.setHeader('Authorization', 'OAuth 001J8wak5KtGmG');
It will fix the "BAD REQUEST" problem. The problem lies in not posting a SOAP XML request, and you can see it if you use SoapUI.
Here's a good link on the subject (the first link here): http://www.salesforce.com/us/developer/docs/api_ajax.pdf
A:
When using the Soap API, you use the HTTPHeader SOAPAction to determine what API method to call. e.g. if you want to call the GetShippingLabel function, you would create a Soap Envelope like this:
x
xxx
Please note that should be above and contains the function you want to call, for example, GetShippingLabel.
You can see that AuthHdr is the name of the Header and RequesterCredentials is the name of the Body (which contains our POST variables, UserID in this case).
Also, make sure to set the Content-Type header in your HTTPRequest to text/xml. You will also need to provide valid values for both Content-Type and Content-Length (also with proper quotes). You can see that in the HTTPResponse object.
Finally, you can see that the Soap Envelope has a body and a header, you should set them both as well.
I did not include the header and body of the HTTPRequest, but it should look like this:
HttpRequest req = new HttpRequest();
req.setHeader('SOAPAction', 'getShippingLabel');
req.setEndpoint('https://api.ebay.com/api/sell/shippingaddress');
req.setMethod('POST');
// Set Header with authorization token
req.setHeader('Authorization', 'OAuth 00Dj0000000sTJN!AR_Rcv!AR_Rcv');
// Set Header to be in XML format
req.setHeader('Content-Type', 'text/xml;charset=UTF