Example Explanation
This example sends a GET request to the Yahoo Weather API and parses the returned JSON response to use thetitle information as header, URL parameter, and body.You can send HTTP requests to external APIs and process responses using JavaScript. This page contains an example of making a REST API call using Apache Commons HttpClient and parsing the response.
var post = new org.apache.commons.httpclient.methods.PostMethod("https://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent("select item from weather.forecast where woeid in (select woeid from geo.places(1) where text='$location')"));
var client = new org.apache.commons.httpclient.HttpClient();
post.setRequestHeader("Content-type", "application/json");
post.setRequestHeader("Accept", "application/json");
var status = client.executeMethod(post);
var br = new java.io.BufferedReader(new java.io.InputStreamReader(post.getResponseBodyAsStream()));
var response = "";
var line = br.readLine();
while(line != null){
response = response + line;
line = br.readLine();
}
post.releaseConnection();
var item = JSON.parse(response);
requestHeaderMapFromClient.put("title" ,item.query.results.channel.item.title);
requestUrlParamMapFromClient.put("title" ,item.query.results.channel.item.title );
bodyText= 'title:'+ item.query.results.channel.item.title;
title information as header, URL parameter, and body.