Our new Appfire Documentation Space is now live!

Take a look here! If you have any questions please email support@appfire.com

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 12 Current »

Abstract

This code snippet makes an HTTP call and processes the returned data.

Logic

  • Import the HTTPBuilder and the request methods.

  • Make an HTTP by creating a new HTTP instance.

  • Request the data passing the respective method, the content type, and the request configuration closure to the request method.

  • Return the parsed data from the response.

Snippet 

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.<methodName>
import static groovyx.net.http.ContentType.<contentType>
 
// initialize a new builder and give a default URL
def http = new HTTPBuilder("<URL>")
 
def data = http.request(<methodName>,<contentType>) { req ->
 
  response.success = { resp, reader ->
    assert resp.status == 200
	return reader
  }
 
  // called only for a 404 (not found) status code:
  response."404" = { resp ->
    log.error ("Not found")
  }
}

if (data) {
  // process returned data
}
Placeholders

Placeholder

Description

Example

<URL>

URL

https://www.google.com/

<methodName>

Name of the request method

GET

<contentType>

Type of the content requested for

TEXT

Context

The outcome of the code snippet depends on the content type passed to the request method. You could use this code, for example, to get a specific currency conversion rate.

Example

To get the HTML text an issue view page, you can use this snippet to make the HTTP call, get the content and parse it.

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT
 
// initialize a new builder and give a default URL
def http = new HTTPBuilder("https://www.google.com/")
 
return http.request(GET,TEXT) { req ->
  response.success = { resp, htmlText ->
    assert resp.status == 200
    log.debug("My response handler got response: ${resp.statusLine}")
    if(htmlText){
     return htmlText.getText()
    }
    else{
      return null
    }
  }
 
  // called only for a 404 (not found) status code:
  response."404" = { resp ->
    log.error ("Not found")
    return null
  }
}

Reference

Filter by label

There are no items with the selected labels at this time.


  • No labels