Skip to content
Solimando Damien edited this page Nov 18, 2015 · 37 revisions

show.http method exposes a JQuery like HTTP client ($.ajax({...})).

The following parameters are supported as options:

  • url
  • data
  • headers
  • type
  • username
  • password
  • processData

The following additional parameters are supported

  • processResponse allowing to control the automatic conversion of response payloads based on Content-type HTTP header.

The method response

The returned object is a response promise.

The promise implements the following methods

  • done(data, textStatus, response) where

    • data is the response payload automatically deserialised based on the response Content-type
    • textStatus is the text representation of the HTTP response status (OK, NOT_FOUND,...)
    • response is an Map/Object/Dictionary containing the following properties:
      • statusCode the HTTP response status
      • encoding the payload encoding
      • contentType the content type of the payload
      • headers the response HTTP headers
  • fail(error, errorMessage,) where

    • error the Java Exception thrown by the client
    • errorMessage is a string representation of the exception
  • then(data, textStatus, response) has the same signature as the done method.

Examples

HTTP POST method with form-urlencoded payload (Groovy)

def options = [
  url: 'http://localhost:8080/test',
  type: 'POST',
  headers:[
    'Content-type':"application/x-www-form-urlencoded"
  ],
  data:[
    name:'Damien',
    age:8
  ]
]

show.http(options).then { data, textStatus, response ->
  data
}

HTTP POST method with JSON payload (Javascript)

var options = {
  url: 'http://localhost:8080/test',
  type: 'POST',
  headers:{
    'Content-type':'application/json'
  },
  data:{
    name:'Damien',
    age:8
  }
}

show.http(options).then( function(data, textStatus, response) {
  return data.name + " " + data.age
}

HTTP POST method with XML payload (Python)

options = {
  'url': 'http://localhost:8080/test',
  'type': 'POST',
  'headers':{
    'Content-type':'application/xml',
    'Accept':'application/xml'
   },
   'data':{
     'name':'Damien',
     'age':8
   }
}

def done(data, status, response):
    print data.getElementsByTagName("name")

def fail(response, status, error):
    error.printStackTrace()

show.http(options).done(done).fail(fail)

Secure requests

The show.httpmethod allows to make https SSL requests (eventually signed using either jks, keycert or pkcs12 signing methods)

The ssl option key will be used in order to configure the secure layer. If requests must be signed, required certificates must be accessible inside the project (in the /show folder)

SSL connection on a trusted HTTPS Server

In order to connect to a trusted HTTP server (a server whose certificates have been signed by a trusted CA), just use https as protocol in the url option in place of http

Groovy Example

show.http([
  url: 'https://www.google.com'
]).done { data, textStatus, response ->
  data
}

SSL connection on a untrusted HTTPS Server

If the HTTPS server is untrusted, by default connections will be rejected for security reasons. In order to force the connection, use the rejectUnauthorized option.

Javascript example

show.http({
	url: 'https://my-untrusted-server:443',
	ssl: {
		rejectUnauthorized:false
	}
}).then( function(data, textStatus, response) {
  return data
}

SSL connection on an HTTPS Server with a provided CA certificate

In custom PKI infrastructures scenarios where server certificates has been signed by a custom CA, you can use the ssl ca option to specify the CA certificate used to sign the HTTPS server certificate.

options = {
  url: 'https://my-untrusted-server:443',
	ssl: {
		ca:'ca/ca.crt'
	}
}

def done(data, status, response):
    print data

Signing the client requests

In some scenarios clients are required to sign their requests. Hot supports the JKS, PKCS12 and key/cert certificate file formats.

JKS

JKS (Java keystore) is a container for authorization certificates or public key certificates.

You can sign HTTPS requests with keys/certificates stored in a JKS with the following options:

  • jks the location of the keystore file in the project (relative to the /show directory)
  • jksPassword the password of the keystore
  • jksCertificatePassword the certificate password (optional, use jksPassword if empty)

Javascript Example

show.http({
	url: 'https://my-untrusted-server:443',
	ssl: {
		ca:'ca/ca.crt',
		jks:"ca/client.jks",
		jksPassword:"clientclient",
		jksCertificatePassword:"client"
	}
}).then( function(data, textStatus, response) {
  return data
})

Keycert

You can sign your requests with a pair of private/public key files (.key and .crt files).

Groovy Example

show.http([
	url: 'https://my-untrusted-server:443',
	ssl: [
		ca:'ca/ca.crt',
		key:"ca/client.key",
		cert:"ca/client.crt",
		passphrase:"client"
	]
]).then { data, textStatus, response ->
   data
}

PKCS12

Finally you can sign your requests with keys/certificates stored in the PKCS12 file format.

JS example

show.http({
	url: 'https://my-untrusted-server:443',
	ssl: {
		ca:'ca/ca.crt',
		p12:"ca/client.p12",
		passphrase:"client"
	}
}).then( function(data, textStatus, response) {
  return data
})

Clone this wiki locally