Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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 

Code Block
languagegroovy
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
}

...

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.

...

Filter by label (Content by label)
showLabelsfalse
max5
spacesKB
showSpacefalse
sortmodified
typepage
reversetrue
cqllabel in ( "jmwe-groovy" , "kb-code-snippet" , "jmwe-server" ) and type = "page" and space = "KBJMWE"
labelsjmwe-nunjucks jmwe-cloud

...