Örnek Açıklaması
Bu örnek, Yahoo Weather API’sine bir GET isteği gönderir ve dönen JSON yanıtını parse ederektitle bilgisini header, URL parametresi ve body olarak kullanır.JavaScript kullanarak harici API’lere HTTP istekleri gönderebilir ve yanıtları işleyebilir. Bu sayfa, Apache Commons HttpClient kullanarak REST API çağrısı yapma ve yanıtı parse etme örneğini içerir.
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 bilgisini header, URL parametresi ve body olarak kullanır.