public class HttpRequest extends BaseRequest
Connection
.
Typically, a request is created in one of the following ways:
HttpResponse<String> jsonResponse = operation.application.getConnection("Job Definitions").get().asString() println ("Response: $jsonResponse.body")
HttpResponse<String> jsonResponse = operation.application.getConnection("App Connection").get("/jobdefinitions").asString() println ("Response: $jsonResponse.body")
HttpResponse<String> jsonResponse = operation.application.getConnection("App Connection").get().pathParam("resourcename", "jobdefinitions").asString() println ("Response: $jsonResponse.body")
In additon to GET, HttpRequest also supports other HTTP methods such as POST:HttpResponse<String> jsonResponse = operation.application.getConnection("Job Definitions").get() .queryParam("q", json(["jobType" : "Rules"])).asString()
Requests can fail or return unexpected results for a number of reasons. Here is an example call from Groovy to check for and handle errors. This example uses JsonSlupper to parse the response Json:HttpResponse<String> jsonResponse = operation.application.getConnection("Job").post() .header("Content-Type", "application/json") .body(json(["jobType" : "Rules", "jobName" : "LongRunningRule"])).asString()
Example call from Groovy to push data from Data Management (DM) into Planning and wait for the DM job to complete. This example uses JsonPath to parse the response Json. See JsonPath for usage:HttpResponse<String> jsonResponse = operation.application.getConnection("Job Definitions").get().asString() // Check if the HTTP status code is successful if(!(200..299).contains(jsonResponse.status)) throwVetoException("Error occured: " + jsonResponse.statusText) // Check if content type of the response is json if(!(jsonResponse.headers?.getFirst("Content-Type") ==~ /application\/json.*/)) throwVetoException("Error occured: Unexpected content type in response: ${jsonResponse.headers?.getFirst('Content-Type')}") def object = new JsonSlurper().parseText(jsonResponse.body) as Map println "Number of jobs returned: ${((List)object.items).size()}"
// Call DM POST REST API to load product volumes from DM to Planning. HttpResponse<String> jsonResponse = operation.application.getConnection("DM").post() .body(json(["jobType":"DATARULE", "jobName":"ProductVolumes", "startPeriod":"Jan-17", "endPeriod":"Dec-17", "importMode":"REPLACE", "exportMode":"STORE_DATA", "fileName":"inbox/ProductVolumes.csv"])) .asString(); def status = awaitCompletion(jsonResponse, "DM") println("Push Product Volumes to Planning ${status == 0 ? "successful" : "failed"}.\n") // Wait for DM job to be completed def awaitCompletion(HttpResponse<String> jsonResponse, String connectionName) { final int IN_PROGRESS = -1 if(!(200..299).contains(jsonResponse.status)) throwVetoException("Error occured: $jsonResponse.statusText") // Parse the JSON response to get the status of the operation. Keep polling the DM server until the operation completes. ReadContext ctx = JsonPath.parse(jsonResponse.body) int status = ctx.read('$.status') for(long delay = 50; status == IN_PROGRESS; delay = Math.min(1000, delay * 2)) { sleep(delay) status = getJobStatus(connectionName, (String)ctx.read('$.jobId')) } return status } // Call DM GET REST API to poll the DM server for job status int getJobStatus(String connectionName, String jobId) { HttpResponse<String> pingResponse = operation.application.getConnection(connectionName).get(jobId).asString() return JsonPath.parse(pingResponse.body).read('$.status') }
Supported types of authentication include None, Basic and API Key. Additional types of authentication such as OAuth will be added in the future.
For more details on how to create, edit and delete named connections, see Connecting Environments in EPM Cloud section within the Planning and Budgeting Cloud Administering Planning documention.
Modifier and Type | Field and Description |
---|---|
protected RequestBodyEntity |
bodyEntity
Body for this request if it supports one.
|
connection, httpRequest
Modifier and Type | Method and Description |
---|---|
RequestBodyEntity |
getBodyEntity()
Returns the
RequestBodyEntity for this request if it supports
a body. |
java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
getHeaders()
Returns the headers sent/received to/from the resoure this Http request
is for.
|
HttpMethod |
getHttpMethod()
Returns the method to be performed on a resource this Http request
is for.
|
protected java.lang.String |
getPassword()
Returns the password for the user making this request.
|
java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
getQueryParams()
Returns the query parameters for this request.
|
java.lang.String |
getUrl()
Return the URL for the resource this Http request is for.
|
java.lang.String |
getUserName()
Returns the name of the user making this request.
|
HttpRequest |
header(java.lang.String name,
java.lang.String value)
Returns this request after setting the specified header.
|
HttpRequest |
headers(java.util.Map<java.lang.String,java.lang.String> headerMap)
Returns this request after setting the specified headers.
|
static boolean |
isLocalHost(java.lang.String hostName)
Returns true if the specified host name is that of the local host.
|
HttpRequest |
pathParam(java.lang.String name,
java.lang.String value)
Returns this request after setting the sepcified dynamic parameter
value in the URL.
|
HttpRequest |
queryParam(java.util.Map<java.lang.String,?> parameters)
Deprecated.
|
HttpRequest |
queryParam(java.lang.String name,
java.util.Collection<?> value)
Returns this request after setting the specified query parameter.
|
HttpRequest |
queryParam(java.lang.String name,
java.lang.Object value)
Returns this request after setting the specified query parameter.
|
HttpRequest |
queryParams(java.util.Map<java.lang.String,?> parameters)
Returns this request after setting the specified query parameters.
|
asString, getHttpRequest
protected RequestBodyEntity bodyEntity
public RequestBodyEntity getBodyEntity()
RequestBodyEntity
for this request if it supports
a body.
A request with a method of POST/PUT will likely have a body.
RequestBodyEntity
for this request if it supports
a bodypublic java.util.Map<java.lang.String,java.util.List<java.lang.String>> getHeaders()
public HttpMethod getHttpMethod()
protected java.lang.String getPassword()
public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getQueryParams()
public java.lang.String getUrl()
public java.lang.String getUserName()
public HttpRequest header(java.lang.String name, java.lang.String value)
The request headers will be merged into the default headers configured/saved with the connection. The request header will win if a header with the same name exists in both the default and the request headers.
name
- name of the headervalue
- value for the headerpublic HttpRequest headers(java.util.Map<java.lang.String,java.lang.String> headerMap)
This method can be called to set multiple request headers at once.
The request headers will be merged into the default headers configured/saved with the connection. The request header will win if a header with the same name exists in both the default and the request headers.
headerMap
- map of headers and the correponding valuespublic static boolean isLocalHost(java.lang.String hostName) throws java.lang.Exception
hostName
- name of the hostjava.lang.Exception
public HttpRequest pathParam(java.lang.String name, java.lang.String value)
This method can be called to set the value for dynamic parameters if there are placeholders in the URL configured in the connection.
name
- name of the dynamic parametervalue
- value for dynamic parameter@Deprecated public HttpRequest queryParam(java.util.Map<java.lang.String,?> parameters)
This method can be called to set multiple query parameters at once.
The query parameters will be merged into the default query parameters configured/saved with the connection. The request query parameters will win if a query parameters with the same name exists in both the default and the request query parameters.
This method has been deprecated and replaced by
queryParams(Map)
parameters
- map of query parameters and the correponding valuespublic HttpRequest queryParam(java.lang.String name, java.util.Collection<?> value)
This method will set multiple query parameters with the same name and a different value for each value specified in the collection.
The query parameters will be merged into the default query parameters configured/saved with the connection. The request query parameters will win if a query parameters with the same name exists in both the default and the request query parameters.
name
- name of the query parametervalue
- value for the query parameterpublic HttpRequest queryParam(java.lang.String name, java.lang.Object value)
The query parameter will be merged into the default query parameters configured/saved with the connection. The request query parameters will win if a query parameters with the same name exists in both the default and the request query parameters.
name
- name of the query parametervalue
- value for the query parameterpublic HttpRequest queryParams(java.util.Map<java.lang.String,?> parameters)
This method can be called to set multiple query parameters at once.
The query parameters will be merged into the default query parameters configured/saved with the connection. The request query parameters will win if a query parameters with the same name exists in both the default and the request query parameters.
parameters
- map of query parameters and the correponding valuesCopyright © 2017, 2024, Oracle and/or its affiliates. All rights reserved.