0

Trying to apply abs_path from RFC2616 (https://www.rfc-editor.org/rfc/rfc2616#section-5.1.2) but receiving callout exception 'System.CalloutException: no protocol: /animals'. End point url trying in following example : http://th-apex-http-callout.herokuapp.com/animals

eg.

enter image description here

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('Host','th-apex-http-callout.herokuapp.com'); 
request.setEndpoint('/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserializes the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');
    System.debug('Received the following animals:');
    for (Object animal: animals) {
        System.debug(animal);
    }
}

expected output : Received the following animals: majestic badger fluffy bunny etc...

Community
  • 1
  • 1
user2200278
  • 95
  • 1
  • 4
  • 10

1 Answers1

0

I've never tried sending Host as a header but this works just fine

request.setEndpoint('http://th-apex-http-callout.herokuapp.com/animals');

eyescream
  • 18,088
  • 2
  • 34
  • 46
  • Yes this works. I wanted to find why 'Host' header field isn't working ? Tried with postman also same error. Do i need to set any other header field ? – user2200278 Apr 01 '21 at 04:15