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
karnadesaikarnadesai 

HTTP POST Callout to .NET Web Service

Hello Everyone,

 

I have created a RESTFUL .NET Web Service using WCF. I want to integrate this web service within Salesforce.

My code for .NET and Salesforce is below. The problem i am facing right now is that whenever i populate the body and pass XML data my .NET web service returns bad request error. For testing my .NET Web service i had created a client in C# and also used tool called fiddler. It works fine for both of them.

 

My .NET Code,

 

Code for IService

 

[OperationContract]
[WebInvoke(UriTemplate = "getXML", Method = "POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
String getXML(XElement response);

 

 

Code for Service

public String getXML(XElement response)
{
return "Hello World";
}

 

My Salesforce APEX Code is,

 

 HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setHeader('Content-Type', 'text/xml');

req.setCompressed(true); // otherwise we hit a limit of 32000
req.setMethod('POST');
req.setHeader('Accept', 'text/xml;charset=utf-8');
String email='karnadesai@gmail.com';
string firstName='karna';
String requestString = '<list>'+
'<user>'+'<userId>0</userId>'+'<userName>othmanelmoulat</userName>'+
'<password>123qwe</password>'+'<mainRoleId>5</mainRoleId>'+
'<contact>'+'<email>'+email+'</email>'+
'<firstName>'+firstName+'</firstName></contact>'+
'</user></list>';

req.setHeader('Content-Length',String.valueOf(requestString.length()));
req.setBody(requestString);

Http http = new Http();
HttpResponse res = http.send(req);

System.debug(res.getBody());

 

I get bad request error only when i set the body of request. I have to pass data using XML only to this web service. Thanks for your help

Best Answer chosen by Admin (Salesforce Developers) 
karnadesaikarnadesai

Problem Solved.

For some reason .NET does not expect compressed data and also there is no need setting content length explicitly.


Remove Following Lines

req.setCompressed(true); // otherwise we hit a limit of 32000
req.setHeader('Content-Length',String.valueOf(requestString.length()));