Can't get API to work

Hello! Working on a little project, was hoping someone could help me out. I’ve been banging my head against the wall for days trying to get this fetch request to an API to work. The API is EmailRep at Introduction - Introduction. I keep getting 403 responses and I’m assuming it has to do with credentials. I’ve tried putting them in a header and everywhere else but I keep getting “failed to fetch” or 403 errors. Any help would really be appreciated. Thanks!

 const submitHandler = (e) => {
    if (!validator.isEmail(emailToCheck)) {
      alert("Enter valid email!");
      setIsSafe("neutral");
    } else {
      e.preventDefault();
      fetch(
        "https://cors-anywhere.herokuapp.com/https://emailrep.io/example@gmail.com",
        {
          method  : 'GET',
          headers: {
            "User-Agent": "RKD412",
          },
        }
      ).then(
        (result) => {
          setItems(result);
          setIsLoaded(true);
          console.log(result);
        },

        (error) => {
          setError(error);
          setIsLoaded(true);
          console.log(error);
        }
      );
    }
  };

I think the reason is that you’re using the demo app for cors-anywhere (https://cors-anywhere.herokuapp.com), although it could be something to do with checking at the originating server end (it might not like the proxy). It’s severely rate-limited to ensure you can’t keep doing what you’re trying to do. It will allow a request through every so often. Most of the time,if you make repeated requests (normal when you’re seeing if something works), it won’t. If you want a CORs proxy, you could do worse than following the instructions he gives to deploy your own CORs proxy, or else find another service.

1 Like

Thank you for your response! Is a CORs proxy even something I need for this particular API? I just threw it on there as an attempt to see if the fetch would then work. When I remove it I know longer get the 403 response but simply a “TypeError: Failed to fetch”. I’ll try to see about deploying my own CORs proxy as well. Not sure why this API is giving me so much trouble, it seemed much simpler than the other ones I have been messing around with this week.

This would indicate something else: I can’t see anything immediately wrong in the code, when you try it what does the information on the network tab in your browser tools say?

1 Like

Thanks again for the help! I’ll copy what was in the Network tab below in the Headers section. The response section simply says “Failed to load response data: no data found for resource with given identifier.”

1. Request URL:
https://emailrep.io/example@gmail.com

2. Request Method:
GET

3. Status Code:
200

4. Referrer Policy:
strict-origin-when-cross-origin

1. Response HeadersView source

1. Connection:
keep-alive

2. Content-Encoding:
gzip

3. Content-Type:
text/html

4. Date:
Mon, 25 Oct 2021 19:49:48 GMT

5. ETag:
W/"5f848eae-4efb"

6. Last-Modified:
Mon, 12 Oct 2020 17:13:18 GMT

7. Server:
nginx/1.10.3 (Ubuntu)

8. Transfer-Encoding:
chunked

2. Request HeadersView source

1. Accept:
3./*

 2. Accept-Encoding:
gzip, deflate, br

3. Accept-Language:
en-US,en;q=0.9

4. Connection:
keep-alive

5. Host:
emailrep.io

6. Origin:
http://localhost:3000

7. Referer:
http://localhost:3000/

8. sec-ch-ua:
"Chromium";v="95", ";Not A Brand";v="99"

9. sec-ch-ua-mobile:
?0

10. sec-ch-ua-platform:
"Linux"

11. Sec-Fetch-Dest:
empty

12. Sec-Fetch-Mode:
cors

13. Sec-Fetch-Site:
cross

You can test the endpoint in their docs, here: Query an email - /{email} - Reference - Technical Reference - Introduction

When you toggle on the Send requests directly from the browser (CORS must be enabled) option under Test Request and make a request directly from the browser it will get blocked due to CORS settings.

If you check the failed response message it is : “Failed to load response data: no data found for resource with given identifier.”

You could try to set up your own CORS proxy as Dan mentioned in this comment.

1 Like

Awesome, thank you both for your responses! It really helped me out a lot.