Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS error while making post request to logstash

I have configured Logstash 1.5.2 to consume http input on a linux machine.

This is my logstash input configuration:

input {
        http {
                host => "10.x.x.120"
                port => "8500"
        }
}

I can send data to logstash by using curl -XPOST from the linux machine.

But when I make a $http.post(url, postData); request from my angularJS application I get the following error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource

I have hosted my application on the same linux machine using nginx within a docker container. I have tried to configure nginx to allow CORS by adding the following lines to the nginx.conf:

    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

But still the error persists.

Also when I hit http://10.x.x.120:8500 from my browser address bar I get 'ok'.

enter image description hereenter image description here Any help is highly appreciated. Thanks.

like image 818
JSNinja Avatar asked Jul 17 '15 12:07

JSNinja


People also ask

How do I fix the CORS error?

Cross-Origin Resource Sharing (CORS) errors occur when a server doesn't return the HTTP headers required by the CORS standard. To resolve a CORS error from an API Gateway REST API or HTTP API, you must reconfigure the API to meet the CORS standard.

How do you avoid CORS errors?

Open a network tab in your console. In the response header look for the Access-Control-Allow-Origin header. If it does not exist then add it as a middleware in the way we discussed above. If it does exist then make sure there is no URL mismatch with the website.

What is the reason for CORS error?

If the CORS configuration isn't setup correctly, the browser console will present an error like "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at $somesite" indicating that the request was blocked due to violating the CORS security rules.


1 Answers

This could be a solution depending on your setup of Logstash in our case we use the http plugin to accept http calls. The http plugin plugin supports the following configuration options:

http {
    response_headers {
        "Content-Type" => "application/x-www-form-urlencoded",
        "Access-Control-Allow-Origin" => "*",
    }
}
like image 63
aUXcoder Avatar answered Sep 20 '22 06:09

aUXcoder