5

I'm trying to upload an image to imgur with js (browser) and get a CORS error:

Access to XMLHttpRequest at 'https://api.imgur.com/3/upload' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

But preflight request contains Access-Control-Allow-Origin header: enter image description here

The request itself:
enter image description here

What I'm missing? this is because access-control-allow-credentials set to true?

Michael
  • 896
  • 10
  • 28
  • 1
    The screenshot clearly shows `access-control-allow-origin: *` but the error says it's not there. I suspect the error is referring to a different request. – Sidney Apr 17 '19 at 17:46
  • Thanks, I added also a screenshot of the request itself. maybe I don't understand something with the CORS workflow. – Michael Apr 17 '19 at 17:50
  • The request screenshot doesn't seem to include the response headers? – Sidney Apr 17 '19 at 17:54
  • The browser blocks the response because of the CORS policy. – Michael Apr 17 '19 at 17:56
  • 1
    Since the preflight request succeeded with CORS headers, the browser should send the POST request. If the POST response doesn't have CORS headers, the browser disallows JavaScript from accessing the response. It should still show in the devtools though. What if you run the same request outside a browser? Maybe in Postman. Do you see CORS headers there? – Sidney Apr 17 '19 at 18:00
  • Those are the response headers of the POST request in Postman: https://i.imgur.com/aJeuA6C.png – Michael Apr 17 '19 at 18:05
  • Sorry, not sure why it isn't working. I think imgur needs to respond with CORS headers, but I don't see any other questions like this. Imgur links to an official example in their API docs, maybe try that and see if it works. – Sidney Apr 17 '19 at 18:13
  • https://apidocs.imgur.com/#authorization-and-oauth checkout the auth in docs for it – Murtaza Hussain Apr 17 '19 at 18:38
  • @MurtazaHussain I don't understand how this link solves my problem. the request is correct, it works via postman – Michael Apr 17 '19 at 19:20

2 Answers2

12

The problem was with their API Docs :\ The URL is: https://api.imgur.com/3/image and not https://api.imgur.com/3/upload as said here: https://apidocs.imgur.com/#c85c9dfc-7487-4de2-9ecd-66f727cf3139

Michael
  • 896
  • 10
  • 28
0

according to doc https://api.imgur.com/3/upload doesnt have necessary header ,

but on the right side ,the curl example use https://api.imgur.com/3/image has Access-Control-Allow-Origin header

and somehow they will check your referer, which means if you are in develop mode like webpack dev mode use localhost:8000 it will always return 429 too many request exception

Evol Rof
  • 2,528
  • 2
  • 22
  • 37