Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing CORS with cURL [duplicate]

I've been implementing CORS in a lil app I have using node-restify to test it out and it turns out that in the browser, the behaviour is as expected, this means, in a different origin with CORS disabled, it doesn't work, if CORS is enabled, it works.

However, the tricky part is that with CURL, it always works! I've been following this question: How can you debug a CORS request with cURL?

I'm doing this:

curl -H 'Origin: http://example.com' http://cors.somewhere.com

And using the node-restify example to debug

var restify = require('restify');

var srv = restify.createServer();
//srv.use(restify.CORS()); // I enable and disable by uncomment line

function foo(req, res, next) {
        res.send("bananas");
        next();
}

srv.put('/foo', foo);
srv.get('/foo', foo);
srv.del('/foo', foo);
srv.post('/foo', foo);

srv.listen(process.env.PORT || 8080);

What am I missing?

Thank you!

like image 929
David Dias Avatar asked Dec 21 '13 17:12

David Dias


People also ask

Is curl affected by CORS?

Therefore, it constitutes a cross-origin request and is blocked by the browser by default. Note: The call using curl works just fine, as CORS only affects XMLHttpRequest calls in the browser.

How do you test if CORS is working?

You can test your API's CORS configuration by invoking your API, and checking the CORS headers in the response. The following curl command sends an OPTIONS request to a deployed API.

How do I check CORS errors?

You can test it with any rest client like POSTMAN Rest Client, or simply you can check it from browser console - > Network tab -> in xhr filter - check the header for the particular request. you can check request and response.


1 Answers

It sounds like you are asking if there's a way to prevent curl from making a request at all. This is impossible. curl can always make a request to the server, with or without CORS.

However, curl can also be used to mimic a browser and verify how your server will react to CORS requests. By using the --verbose flag on curl requests, you can see the HTTP request and response headers, and verify that the CORS headers are working as expected. That is what this question covers: How can you debug a CORS request with cURL?

If CORS is enabled, you should see an Access-Control-Allow-Origin header in the response. If CORS is disabled, you should not see any Access-Control-* headers in the response.

like image 124
monsur Avatar answered Oct 14 '22 02:10

monsur