Copyright© 2008-2023 Sitevision AB, all rights reserved.
O
- script objectF
- script function@Requireable(value={"Requester","JsonRequester"}) public interface Requester<O,F>
This utility supports the official (RFC2616 and RFC5789) request methods: GET, POST, HEAD, PUT, DELETE and PATCH - but not the OPTIONS, TRACE or CONNECT methods. Redirects will only be followed for the GET and HEAD methods.
URL:s with an international domain name (IDN) will be converted (ACE:d) automatically internally by this utility, e.g. invoking get("http://www.ringsjömingel.se") will do a HTTP GET using "http://www.xn--ringsjmingel-9ib.se".
Each supported method in this utility comes in two flavours: a base method with the URL only (see example above) and a method that also allows additional options. These are the supported options:
Some methods have additional options. Consult the documentation of the specific method.
Below is a example of how to use the Requester utility to execute a basic GET and process the JSON response body.
require('Requester')
.get('http://jsonplaceholder.typicode.com/posts')
.done(function(result) {
// GET succeeded, handle JSON response result
var i, count = result.length, item;
for (i = 0; i < count; i++) {
item = result[i];
out.println(item.id + ' - ' + item.title + '<br>');
out.println('<small>' + item.body + '</small><br>');
}
})
.fail(function(message) {
// GET failed, handle error message
out.println(message);
});
Below is another example that also demonstrates the options usage and further callback arguments.
var requester = require('Requester'),
options = {
username: 'thomas',
password: 'nordahl_123',
preemptiveAuthentication: true,
headers: {
'X-custom-header': 'Custom value'
},
data: {
query: 'football',
category: 'all'
}
},
multipartOptions = {
contentType: 'multipart/form-data',
files: {
file: fileNode // sv:file, sv:temporaryFile or sv:image
},
data: {
name: 'foo'
}
};
requester.get('https://arestfulsite.se/endpoint/requires/authentication/search', options)
.done(function(result, statusCode, headers) {
// GET succeeded, handle potential JSON response result appropriately
if (statusCode === 204) {
...
} else {
...
}
...
})
.fail(function(message, status) {
// GET failed, handle appropriately
if (status.statusCode === 401) {
...
}
if (status.headers) {
...
}
if (status.body) {
...
}
...
});
requester.post('https://arestfulsite.se/endpoint', multipartOptions)
...
To send XML as a string entity, you must set the contentType
option to "text/plain". You should typically also set the
Content-Type header
to an appropriate value that the remote system will accept. (Remember - if no Content-Type header is set,
Requester will fallback to the type indicated by the contentType
option - i.e. "text/plain" in this case).
var requester = require('Requester'),
options = {
dataType: 'text',
contentType: 'text/plain',
headers: {
'Content-Type': 'application/xml; charset=UTF-8'
},
data: '<?xml version="1.0" encoding="UTF-8"?><message>Hello backend system</message>'
};
requester.post('https://xmlsite.se/endpoint', options)
.done(function(result, statusCode, headers) {
// POST succeeded, handle XML string response result appropriately
...
})
.fail(function(message, status) {
// POST failed, handle appropriately
...
});
Tip! Use XmlParserUtil to process the XML response string!
To make a request to a protected resource URL, you must set the oauth2
option to a valid sv:oAuth2Configuration. Special
considerations must be made regarding error handling. See error handling below.
var requester = require('Requester'),
oAuth2Configuration = (...),
options = {
oauth2: oAuth2Configuration
};
requester.get('https://resourceserver.com/protected-endpoint', options)
.done(function(result, statusCode, headers) {
// Request succeeded, handle response appropriately
...
})
.fail(function(message, status) {
// Request failed, handle appropriately
...
});
var requester = require('Requester'),
oAuth2Configuration = (...),
options = {
oauth2: oAuth2Configuration,
oauth2Type: 'app'
};
requester.get('https://resourceserver.com/protected-endpoint', options)
.done(function(result, statusCode, headers) {
// Request succeeded, handle response appropriately
...
})
.fail(function(message, status) {
// Request failed, handle appropriately
...
});
Different resource servers returns errors in different format. However, a few common error situations might occur in every single request and needs proper handling for a smooth user experience.
Tip! Use OAuth2 requireable util to perform OAuth2 operations.
Note that outgoing socket connections is a shared (and limited) server resource!
All requests will be handled by connections that comes from a shared http connection pool.
The pool has a fixed number of connections (typically 100) and specified timeouts (typically 5 seconds).
A request will fail if a connection can not be made at all (exhausted pool) or if the external
website doesn't respond in time (request timeout). A failure is typically handled via
RequesterChainable.fail(Object)
.
Important Requireable note! "JsonRequester" is available in all Sitevision versions since 4.2 but "Requester" was introduced as of Sitevision 4.5.5.
REST siblings note!
RestApi
.
RestAppInvoker
.
RestApi
,
RestAppInvoker
Modifier and Type | Method and Description |
---|---|
RequesterChainable<O,F> |
delete(String aURL)
Execute a HTTP DELETE.
|
RequesterChainable<O,F> |
delete(String aURL,
O aOptions)
Execute a HTTP DELETE with options.
|
RequesterChainable<O,F> |
get(String aURL)
Execute a HTTP GET.
|
RequesterChainable<O,F> |
get(String aURL,
O aOptions)
Execute a HTTP GET with options.
|
RequesterChainable<O,F> |
head(String aURL)
Execute a HTTP HEAD.
|
RequesterChainable<O,F> |
head(String aURL,
O aOptions)
Execute a HTTP HEAD with options.
|
RequesterChainable<O,F> |
patch(String aURL)
Execute a HTTP PATCH.
|
RequesterChainable<O,F> |
patch(String aURL,
O aOptions)
Execute a HTTP PATCH with options.
|
RequesterChainable<O,F> |
post(String aURL)
Execute a HTTP POST.
|
RequesterChainable<O,F> |
post(String aURL,
O aOptions)
Execute a HTTP POST with options.
|
RequesterChainable<O,F> |
put(String aURL)
Execute a HTTP PUT.
|
RequesterChainable<O,F> |
put(String aURL,
O aOptions)
Execute a HTTP PUT with options.
|
RequesterChainable<O,F> get(String aURL)
aURL
- the urlRequesterChainable<O,F> get(String aURL, O aOptions)
aURL
- the urlaOptions
- the options, see options example aboveRequesterChainable<O,F> put(String aURL)
aURL
- the urlRequesterChainable<O,F> put(String aURL, O aOptions)
aURL
- the urlaOptions
- the options, see options example aboveRequesterChainable<O,F> post(String aURL)
aURL
- the urlRequesterChainable<O,F> post(String aURL, O aOptions)
aURL
- the urlaOptions
- the options, see options example aboveRequesterChainable<O,F> delete(String aURL)
aURL
- the urlRequesterChainable<O,F> delete(String aURL, O aOptions)
aOptions extension for delete
aURL
- the urlaOptions
- the options, see options example aboveRequesterChainable<O,F> patch(String aURL)
Note! The RFC5789 PATCH method is neither safe nor idempotent as defined by RFC2616.
aURL
- the urlRequesterChainable<O,F> patch(String aURL, O aOptions)
Note! The RFC5789 PATCH method is neither safe nor idempotent as defined by RFC2616.
aURL
- the urlaOptions
- the options, see options example aboveRequesterChainable<O,F> head(String aURL)
Note! The response of a HEAD request does not have a body.
aURL
- the urlRequesterChainable<O,F> head(String aURL, O aOptions)
Note! The response of a HEAD request does not have a body.
aURL
- the urlaOptions
- the options, see options example aboveSitevision - Portal and Content Management Made Easy
Sitevision is an advanced Java enterprise portal product and a portlet container (JSR 286) that implements Java Content Repository (JSR 283).
Copyright© 2008-2023 Sitevision AB, all rights reserved.