6

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

4
  • have you verified that is the correct endpoint? Oct 14, 2013 at 17:15
  • i'm not fluent in REST + SF, but another idea is to check that you are setting the REST header correctly Oct 14, 2013 at 17:16
  • 1
    Problem with REST service, not SFDC consumer. Oct 14, 2013 at 21:28
  • The endpoint is correct, I've called it successfully via Chrome.
    – akarnid
    Oct 15, 2013 at 7:49

3 Answers 3

6
+100

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]
0
1

This is a problem with the REST service, not the Salesforce consumer.

When the JSON is valid, he closes the connection instead of sending you any response.

  1. CURL request:

    $ curl http://212.30.237.35:8080/VerkbokhaldRestService.svc/StofnaVerk \
    > --data '{"Customer": "500269677900", "Name": "TestCallout"}' \
    > --header 'Content-Type: application/json'
    curl: (56) Recv failure: Connection reset by peer
    
  2. Chrome request:

    Socket closed

2
  • Just wrong parameters, my bad. This here payload works: {"NumerVidskiptamanns": "500269677900", "Heiti":"Test"}
    – akarnid
    Oct 15, 2013 at 7:47
  • I've gotten further with this, I'm wondering now how to use the JSONGenerator class to generate the JSON string, since the field names in the service don't respond 1-to-1 to SF field names. Will I have generate it as a string and then use a method to JSONify the payload, since I'm fairly sure that the 400 BAD REQUEST is because I'm sending junk data via my callout that the service endpoint can't parse.
    – akarnid
    Oct 15, 2013 at 9:07
1

The following code seems to work when I execute the Anonymous Apex call:

       String content = '{"NumerVidskiptamanns": "500269677900", "Heiti":"Test"}';

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

     /* JSONGenerator js = JSON.createGenerator(true);
     js.writeStartObject();
     js.writeStringField('NumerVidskiptamanns','500269677900');
     js.writeStringField('Heiti','Test');
     js.writeEndObject();
     */

    req.SetEndpoint('http://212.30.237.35:8080/VerkbokhaldRestService.svc/StofnaVerk');
    req.SetBody(content);
    req.setHeader('Content-Type', 'application/json; charset=utf-8');
    system.debug('++++*****++++'+req.GetBody());
    req.setMethod('POST');
    req.setCompressed(true);

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

     System.debug(res.toString());

Seems that it was sufficient to set the correct header there. At least I get no errors in the debug log.

1
  • 1
    nope, still having problems with this. Anyone with any ideas? The answer from the server is always 502 Bad Gateway, but always works when I just use curl on the command line or use a tool like the Advanced REST Client. Like for example, the following works for me : curl 212.30.237.35:8080/VerkbokhaldRestService.svc/StofnaVerk --data '{"NumerVidskiptamanns": "500269677900", "Heiti":"Test"}' --header 'Content-Type: application/json; charset=utf-8'
    – akarnid
    Oct 16, 2013 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .