-
Notifications
You must be signed in to change notification settings - Fork 4
HTTP client
show.http method exposes a JQuery like HTTP client ($.ajax({...})).
The following parameters are supported as options:
urldataheaderstypeusernamepasswordprocessData
The following additional parameters are supported
-
processResponseallowing to control the automatic conversion of response payloads based onContent-typeHTTP header.
The returned object is a response promise.
The promise implements the following methods
-
done(data, textStatus, response)where-
datais the response payload automatically deserialised based on the responseContent-type -
textStatusis the text representation of the HTTP response status (OK, NOT_FOUND,...) -
responseis an Map/Object/Dictionary containing the following properties:-
statusCodethe HTTP response status -
encodingthe payload encoding -
contentTypethe content type of the payload -
headersthe response HTTP headers
-
-
-
fail(error, errorMessage,)where-
errorthe Java Exception thrown by the client -
errorMessageis a string representation of the exception
-
-
then(data, textStatus, response)has the same signature as thedonemethod.
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
}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
}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)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)
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
show.http([
url: 'https://www.google.com'
]).done { data, textStatus, response ->
data
}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.
show.http({
url: 'https://my-untrusted-server:443',
ssl: {
rejectUnauthorized:false
}
}).then( function(data, textStatus, response) {
return data
}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 dataIn some scenarios clients are required to sign their requests. Hot supports the JKS, PKCS12 and key/cert certificate file formats.
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:
-
jksthe location of the keystore file in the project (relative to the/showdirectory) -
jksPasswordthe password of the keystore -
jksCertificatePasswordthe certificate password (optional, usejksPasswordif empty)
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
})You can sign your requests with a pair of private/public key files (.key and .crt files).
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
}Finally you can sign your requests with keys/certificates stored in the PKCS12 file format.
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
})