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
New_DeveloperNew_Developer 

Help with POST method in Webservices

I need to use wenservices to enter data to the external satem. I wrote a triggre and used @future in the Apex class to do this.But iam getting the response as [Status=Bad Request, StatusCode=400]. Not sure how to set the body for POSt request with application/soap+xml content type.  Below is my code. An help is appreciated

HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();

req.setEndpoint('https://testurl');
req.setMethod('POST');

String username = 'test';
String password = 'test';


Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setTimeout(100000);
req.setHeader('Content-Type', 'application/soap+xml; charset=utf-8');
req.setCompressed(false);
req.setBody('Customer= 500269677900& Name=TestCallout');


try {
    res = http.send(req);
} catch(System.CalloutException e) {
    System.debug('Callout error: '+ e);
}

Thanks

Vamsi KrishnaVamsi Krishna
you can construct your body using EncodingUtil.urlEncode

something like this
String strBody =
EncodingUtil.urlEncode('Customer', 'UTF-8') + '=' + EncodingUtil.urlEncode('500269677900', 'UTF-8') + '&' +
EncodingUtil.urlEncode('Name', 'UTF-8') + '=' + EncodingUtil.urlEncode('TestCallout', 'UTF-8');

so that it gets generated in the format <name>=<value>&<name>=<value>
Andries.NeyensAndries.Neyens
Seems weird to me that you are calling a SOAP api without any soap in the body?

The body should contain something like this:

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
  </soap:Header>
  <soap:Body>
    <m:GetStockPrice xmlns:m="http://www.example.org/stock">
      <m:StockName>IBM</m:StockName>
    </m:GetStockPrice>
  </soap:Body>
</soap:Envelope>
New_DeveloperNew_Developer
Thanks Andries. I made the body to be like what you have described. I still get the error as Internal Server error , status code =500. The WSDL file only has SOAP12 binding in it. and salesforce doesnot support soap1.2 . Would that be the reason this is not working??

Thanks in advance for your help.


Andries.NeyensAndries.Neyens
'Hi, no problem with salesforce on that side!

If you have a WSDL and it is not too complex, use the WSDL to Apex: http://help.salesforce.com/HTViewHelpDoc?id=code_wsdl_to_package.htm&language=en_US (http://help.salesforce.com/HTViewHelpDoc?id=code_wsdl_to_package.htm&language=en_US)



For soap 1.2 you need to include this in the header:

oHTTPRequest.setHeader('Content-Type', 'application/soap+xml;charset=UTF-8'

);

for 1.1 you need:

 

oHTTPRequest.setHeader('Content-Type', 'text/xml;charset=UTF-8');

oHTTPRequest.setHeader('SOAPAction', 'some sort of action');

 

 


New_DeveloperNew_Developer
Hi Andries.,

Thanks for the reply. I tried generating apex from WSDL but it is throwing me an error saying cannot find soap1.1 address.

I also have set the header set as you mentioned above.Below is my complete class and when i do post request i get the error "The message with To '' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher.  Check that the sender and receiver's EndpointAddresses agree". along with Status code = 500 and Internal server error

@Future(callout=true)
global static void sendCommunication(Integer ChildNumber, String CommType, Integer ClientID) {

HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();

req.setEndpoint('https://xxx.xx');
req.setMethod('POST');

String username = test';
String password = 'test';

Blob headerValue = Blob.valueOf(username + '' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setTimeout(100000);
req.setHeader('Content-Type',  'application/soap+xml; charset=utf-8');
req.setCompressed(false);

String reqBody = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:plan="http://schemas.datacontract.org/2004/07/">'
   +'<soap:Header/>'+
   '<soap:Body>'+
      '<tem:AddCommunicationItem>'+
        '<tem:lstCommunicationItems>'+
            '<plan:CommunicationItem>'+
                        '<plan:CommunicationType>CommType</plan:CommunicationType>'+
                  '<plan:Description>Testing123</plan:Description>'+
                        '<plan:SCNumber>ChildNumber</plan:SCNumber>'+
               '<plan:SPNumber>ClientID</plan:SPNumber>'+
            '</plan:CommunicationItem>'+
         '</tem:lstCommunicationItems>'+
       '</tem:AddCommunicationItem>'+
   '</soap:Body>'+
'</soap:Envelope>';

req.setBody(reqBody);


try {
    res = http.send(req);
} catch(System.CalloutException e) {
    System.debug('Callout error: '+ e);
}
System.debug(res.getBody());

}

Thanks
Andries.NeyensAndries.Neyens

I'm using GET instead of the POST.

Try to see if on the receiver side is more logging available, seems to be a WCF error...
Maybe [ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)] could solve it