[SalesForce] Error when doing a test callout to a REST web service

I'm trying to test a http callout from the developer console but I'm getting a 502 Bad Gateway error when I try to do a POST. Only working with the sample callout code mentioned here: http://wiki.developerforce.com/page/Apex_Web_Services_and_Callouts

    HttpRequest req = new HttpRequest(); 
    String content = '{"Customer": "500269677900", "Name":"TestCallout"}';
    String endp = 'http://212.30.237.35:8080/VerkbokhaldRestService.svc/StofnaVerk';
    //Set HTTPRequest Method
    req.setMethod('POST');

   //Set HTTPRequest header properties
   req.setHeader('content-type', 'application/json; charset=utf-8');
   req.SetEndpoint('http://212.30.237.35:8080/VerkbokhaldRestService.svc/StofnaVerk');
   req.setMethod('POST');


   //Set the HTTPRequest body   
   req.setBody(content);    

   Http http = new Http();

 try {

    //Execute web service call here     
    HTTPResponse res = http.send(req);  

    //Helpful debug messages
    System.debug(res.toString());
    System.debug('STATUS:'+res.getStatus());
    System.debug('STATUS_CODE:'+res.getStatusCode());

 } catch(System.CalloutException e) {
System.debug('*****'+e.getMessage());
 }      

I get a 400 Bad Request error with the above, since the server expects json I thought it would set encode the response appropiately. If I insert {} into the request body I get a 502 Bad Gateway. The same test call with the same body works fine in a rest resting tool like the Advanced REST Client.

the following works fine on the command line and returns a JSON response:

  curl http://212.30.237.35:8080/VerkbokhaldRestService.svc/StofnaVerk --data '{"NumerVidskiptamanns": "500269677900", "Heiti":"Test"}' --header 'Content-Type: application/json; charset=utf-8'

Any pointers or ideas for me?

edit: posted updated code

Best Answer

Turn off compression, this worked fine for me from from anonymous apex.

String content = '{"NumerVidskiptamanns": "500269677900", "Heiti":"Test"}';
HttpRequest req = new HttpRequest();
Http http = new Http();
req.SetEndpoint('http://212.30.237.35:8080/VerkbokhaldRestService.svc/StofnaVerk');
req.SetBody(content);
req.setHeader('Content-Type', 'application/json; charset=utf-8');
req.setMethod('POST');
HttpResponse res = http.send(req);
System.debug(res.toString());

The debug log shows

10:41:35:829 USER_DEBUG [9]|DEBUG|System.HttpResponse[Status=OK, StatusCode=200]
Related Topic