When we use Logstash, do we sometimes think of using REST calls to request another microservice? So we can get more data. In the official release of Elastic, we can in the address www.elastic.co/guide/en/lo… Find all current filters. Inside this is a filter called HTTP.

In today’s article, I’ll use an example to show how to use this HTTP filter.

 

I will use the following REST API interface to demonstrate:

http://api.map.baidu.com/telematics/v3/weather?location=beijing&output=json&ak=DdzwVcsGMoYpeg5xQlAFrXQt
Copy the code

As shown above, it is an interface to Baidu Weather. Above we change Beijing to another city, we can get the weather conditions of that city.

Based on the above interface, we create the following Logstash configuration file:

http.conf

input {
    generator {
       message => "beijing"
       count => 1
    }
}

filter {
    http {
        body_format => "json"
        follow_redirects => false
        url => "http://api.map.baidu.com/telematics/v3/weather?location=%{[message]}&output=json&ak=DdzwVcsGMoYpeg5xQlAFrXQt"
        verb => "GET"
        headers => [ "Content-Type", "application/json" ]
        target_body => "weather"
   }
}

output {
    stdout {
    codec => rubydebug
    }
}
Copy the code

Above, we use generator to produce an event. We use HTTP filters to send requests.

Use Logstash to run the configuration file above:

 ./bin/logstash -f http.conf 
Copy the code

The results are as follows:

We can then use any other filter to further manipulate this data. I won’t go into details here.

In addition, in the community, I also saw a plugin for a similar function github.com/lucashennin… . It can do a similar job. If you want to try it out, follow the steps above, and since the plugin isn’t from Elastic, you’ll need to install it separately.