Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP request works with cURL but gets CORS response when made in browser with javascript

When I access the typeform API via browser using $.get or superagent or request or axios, I get the error No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

However, when I user cURL or HTTPIE or Postman to make the same request, it is successful everytime. How to solve this behaviour and how to implement the client version?

like image 716
aayushkapoor206 Avatar asked Apr 29 '16 20:04

aayushkapoor206


People also ask

How can I send a CORS request using cURL?

How can I send a CORS request using Curl? To make a CORS request using Curl, you need to pass an Origin HTTP header that specifies the origin of the request (domain, scheme, or port) other than the destination server address, and optionally the required HTTP methods and response headers.

What is Cors in curl?

CORS is a security mechanism built into (all) modern web-browsers (yes! into your web browser! That’s why your curl calls works fine). It basically blocks all the http requests from your front end to any API that is not in the same “Origin” (domain, protocol, and port—which is the case most of the time).

What is curl and why does it block HTTP requests?

That’s why your curl calls works fine). It basically blocks all the http requests from your front end to any API that is not in the same “Origin” (domain, protocol, and port—which is the case most of the time). Now, how does this mechanism work? Let’s say you have an ‘upload’ button in the UI that suppose to upload some form of data to the API.

How does Cors work with JavaScript?

CORS provides a number of different mechanisms for limiting JavaScript access to APIs. It is often not obvious which mechanism is blocking the request. We are going to build a simple web application that makes REST calls to a server in a different domain.


1 Answers

CORS is enforced client side by your browser. If you are not making the request with a browser CORS is not enforced.

like image 133
bhspencer Avatar answered Nov 15 '22 03:11

bhspencer