0

I am trying to use this api to validate email addresses: https://developer.everest.validity.com/#bc0f96ba-bf72-45c3-82ed-36a2dc356e8a

I have it working fine in postman. I get a 200 response and a JSON body with it. Example response looks like the below:

{
    "meta": {},
    "results": {
        "category": "email_domain_invalid",
        "status": "invalid",
        "name": "Domain Invalid",
        "definition": "The email address is associated with a domain that doesn't exist. For example, evereeeeest.validity.com is not a real domain capable of sending and receiving email. So [email protected] is an invalid email since it pertains to a domain that does not exists.",
        "reasons": [],
        "risk": "very_high",
        "recommendation": "suppress",
        "address": "[email protected]",
        "diagnostics": {
            "role_address": false,
            "disposable": false,
            "typo": true,
            "typo_suggestion": "gmail.com"
        }
    }
}

However, when trying to setup a basic apex REST callout, I again get a 200 response but it is returning an html table. I am very new to REST callouts, so figure I'm either missing something or there is a problem with the API.

Here is my apex callout code:

HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.everest.validity.com/api/2.0/validation/address/[email protected]');
request.setMethod('GET');
request.setHeader('X-API-KEY', '[REDACTED]');
request.setHeader('Content-Type', 'application/json');
HttpResponse response = http.send(request);
System.debug(response.getBody());

When I check the debug it looks like this: enter image description here

As you can the see, the html table has some semblance of correctness in that it starts with "meta" and "results" which are present in the correct JSON body response when using postman.

So it seems something about using Salesforce to do this callout is not working properly. Does anyone know if this is a salesforce issue or an issue with the API?

Thanks

3
  • 1
    Have you tried setting the header Accept in salesforce with application/json ? May 12, 2021 at 6:32
  • That solved it. The syntax is request.setHeader('Accept', 'application/json'); Thanks! Feel free to make it an answer so I can mark it as correct.
    – Ben
    May 12, 2021 at 7:06
  • 1
    Posted ;) (I added the code to make the answer self-contained) May 12, 2021 at 7:11

1 Answer 1

1

Some applications needs you to specify what is the format you expect as a response, and some headers are set by default by PostMan.

Please try setting the header Accept to application/json

eg:

request.setHeader('Accept', 'application/json');

You must log in to answer this question.

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