> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Making API Call and Processing Result

> Making API calls with Groovy script and processing results, transmitting response data to backend as header, URL parameter or body

## Groovy Script

```groovy theme={null}
import groovy.json.JsonSlurper

def location = 'Stockholm, Sweden'
def connection = new URL("https://query.yahooapis.com/v1/public/yql?q=" + 
    URLEncoder.encode("select item " +
    "from weather.forecast where woeid in " +
    "(select woeid from geo.places(1) " +
    "where text='$location')", 'UTF-8' ) )
    .openConnection() as HttpURLConnection

// set some headers
connection.setRequestProperty( 'User-Agent', 'groovy-2.4.4' )
connection.setRequestProperty( 'Accept', 'application/json' )

if ( connection.responseCode == 200 ) {
    // get the JSON response
    def json = connection.inputStream.withCloseable { inStream ->
        new JsonSlurper().parse( inStream as InputStream )
    }
    
    // extract some data from the JSON, printing a report
    def item = json.query.results.channel.item
    
    requestHeaderMapToTargetAPI.put("title",item.title)
    requestHeaderMapToTargetAPI.put("Temperature","${item.condition?.temp}")
    requestHeaderMapToTargetAPI.put("Condition","${item.condition?.text}")
    
    requestUrlParamMapToTargetAPI.put("title",item.title)
    requestUrlParamMapToTargetAPI.put("Temperature","${item.condition?.temp}")
    requestUrlParamMapToTargetAPI.put("Condition","${item.condition?.text}")
    
    bodyText= 'title:'+ item.title + ', temp:' + "${item.condition?.temp}" + ", text:" + "${item.condition?.text}"
    
    // show some forecasts
    def forecast= 'Forecasts:'
    item.forecast.each { f ->
        forecast += ' * ${f.date} - Low: ${f.low}, High: ${f.high}, Condition: ${f.text}'
    }
} else {
    requestHeaderMapToTargetAPI.put("responseCode",connection.responseCode)
    requestHeaderMapToTargetAPI.put("responseText",connection.inputStream.text)
    requestUrlParamMapToTargetAPI.put("responseCode",connection.responseCode)
    requestUrlParamMapToTargetAPI.put("responseText",connection.inputStream.text)
    bodyText='title:'+ item.title + ', code:' + connection.responseCode +", text:" + connection.inputStream.text
}
```

## Explanation

This script performs the following operations:

1. **API Call**: Makes HTTP request to Yahoo Weather API
2. **JSON Parse**: Parses JSON response
3. **Data Extraction**: Extracts weather information
4. **Data Transmission**: Transmits this information to backend as header, URL parameter, or body
5. **Error Management**: Transmits error information in case of error

<Note>
  This script should be run on the request line (Request Policy) because it uses the `requestHeaderMapToTargetAPI` and `requestUrlParamMapToTargetAPI` variables.
</Note>
