biz.neustar.wpm.api
Interface HttpRequest


public interface HttpRequest

An object representing an HTTP request to be made in the future. The actual request is not made until execute() is called.


Method Summary
 void addFileUpload(java.lang.String name, DataFile dataFile)
          Add the contents of a file to the request, which will make this a multipart request and transmit the file contents as a multipart part.
 void addFileUpload(java.lang.String name, DataFile dataFile, java.lang.String contentType)
          Add the contents of a file to the request, which will make this a multipart request and transmit the file contents as a multipart part.
 void addRequestHeader(java.lang.String key, java.lang.String value)
          Adds or sets a HTTP header with the supplied value.
 void addRequestHeaders(NativeObject paramsNO)
          Adds a set of HTTP headers to the HTTP request.
 void addRequestParameter(java.lang.String key, java.lang.String value)
          Adds an HTTP request parameter to be included with the HTTP POST request.
 void addRequestParameters(NativeObject paramsNO)
          Adds a set of HTTP parameters to the HTTP request.
 void appendRequestHeader(java.lang.String key, java.lang.String value)
          Adds a HTTP header with the supplied key and value.
 HttpResponse execute()
          Executes this HTTP request.
 java.lang.String getUrl()
          Get the URL.
 boolean hasHeader(java.lang.String header)
          Check if the request has a header set
 void makeMultiPart()
          Change the request into a multipart request, so that each named parameter is treated as separate part.
 void removeHeader(java.lang.String header)
          Remove an existing header from the request
 void setRequestBody(java.lang.Object body)
          Sets the raw content of the HTTP POST (or PUT) body.
 void setRequestBody(java.lang.Object body, java.lang.String contentType, java.lang.String charSet)
          Convenience function for setting the body of the HTTP and also setting the Content-Type header.
 void setRequestBodyAsBase64EncodedBytes(java.lang.String bodyBase64Encoded)
          Deprecated. Consider using DataFile and/or Bytes along with setRequestBody(Object)
 void setVerificationText(java.lang.String verificationText)
          Instructs the HTTP request to look for this string of content against the response content.
 

Method Detail

addRequestHeader

void addRequestHeader(java.lang.String key,
                      java.lang.String value)
Adds or sets a HTTP header with the supplied value.

If the key already exists this will overwrite it.

Parameters:
key - the HTTP header key.
value - the HTTP header value.

appendRequestHeader

void appendRequestHeader(java.lang.String key,
                         java.lang.String value)
Adds a HTTP header with the supplied key and value.

If the key already exists, a new value is added.

For example the following adds two 'blah' headers.

 var req = c.newGet("http://www.example.com");
 req.appendRequestHeader("blah", "foo");
 req.appendRequestHeader("blah", "bar");
 var response = req.execute();
 

Parameters:
key - the HTTP header key.
value - the HTTP header value.

addRequestHeaders

void addRequestHeaders(NativeObject paramsNO)
Adds a set of HTTP headers to the HTTP request. While the type is documented "NativeObject", this really just means that the argument can be some sort of JavaScript key/value pair. For example, a call might look like the following:
 post.addRequestHeaders({
     header_one: 'some value',
     header_two: ['foo', 'bar']
 });
 

Parameters:
paramsNO - the JavaScript map of headers used when issuing the HTTP request.

removeHeader

void removeHeader(java.lang.String header)
Remove an existing header from the request

Parameters:
header - the header name to remove

hasHeader

boolean hasHeader(java.lang.String header)
Check if the request has a header set

Parameters:
header - the header name to remove
Returns:
true if the header exists

getUrl

java.lang.String getUrl()
Get the URL.

Returns:
the url of the request.

setVerificationText

void setVerificationText(java.lang.String verificationText)
Instructs the HTTP request to look for this string of content against the response content. If it is not found, a value of false will be returned by HttpResponse.isContentMatched().

Parameters:
verificationText - the text to check against the returned content.

addRequestParameter

void addRequestParameter(java.lang.String key,
                         java.lang.String value)
Adds an HTTP request parameter to be included with the HTTP POST request.

Parameters:
key - the HTTP parameter key.
value - the HTTP parameter value.

addRequestParameters

void addRequestParameters(NativeObject paramsNO)
Adds a set of HTTP parameters to the HTTP request. While the type is documented "NativeObject", this really just means that the argument can be some sort of JavaScript key/value pair. For example, a call might look like the following:
 post.addRequestParameters({
     'name1': 'value1',
     'name2': 'value2',
     'name3': ['value3', 'value4']
 });
 

Parameters:
paramsNO - the JavaScript map of parameters used when issuing an HTTP POST.

setRequestBody

void setRequestBody(java.lang.Object body,
                    java.lang.String contentType,
                    java.lang.String charSet)
Convenience function for setting the body of the HTTP and also setting the Content-Type header. See setRequestBody(Object) for more info.

Parameters:
body - the body of the HTTP request.
contentType - the content type of the body.
charSet - the character set encoding (eg: UTF-8).

setRequestBody

void setRequestBody(java.lang.Object body)
Sets the raw content of the HTTP POST (or PUT) body. This function is useful for situations where you aren't submitting a normal URL-encoded form request and instead need to control the body directly. This is most often done when simulating SOAP or REST API calls. The object supplied can be a simple string, a DataFile, or a set of Bytes.

If you are uploading a string, it is assumed to be in UTF-8 character encoding.

If you are uploading a DataFile, please note that this is different than addFileUpload(String, DataFile), which simulates what a browser does for file uploads by making the request a multi-part request.

If you are uploading some Bytes, the most common use case is that those bytes were obtained from Utils.gzip(Object).

Note that while this does automatically set the Content-Length request header, it does not (currently) handle anything related to content types or content encoding, so you must set those headers yourself if they are needed. You may also consider using setRequestBody(Object, String, String) as a shortcut to setting the body and setting the content type header.

Parameters:
body - the body of the HTTP request.

setRequestBodyAsBase64EncodedBytes

void setRequestBodyAsBase64EncodedBytes(java.lang.String bodyBase64Encoded)
Deprecated. Consider using DataFile and/or Bytes along with setRequestBody(Object)

Sets the raw content of the HTTP POST body using bytes represented in a standard base64 encoding. This function is good for sending raw bytes across the wire.

Parameters:
bodyBase64Encoded - the body of the HTTP request in bytes encoded as a base64 string.

execute

HttpResponse execute()
Executes this HTTP request.

Returns:
an object that contains the detailed object results (status code, timings, etc) and whether content verification matched.

addFileUpload

void addFileUpload(java.lang.String name,
                   DataFile dataFile)
Add the contents of a file to the request, which will make this a multipart request and transmit the file contents as a multipart part. Tries to detect the Content-Type of the uploaded file.

Parameters:
name - the request part name
dataFile - the file to use as the content body. You can get a handle to an uploaded file using test.getFile(String)

addFileUpload

void addFileUpload(java.lang.String name,
                   DataFile dataFile,
                   java.lang.String contentType)
Add the contents of a file to the request, which will make this a multipart request and transmit the file contents as a multipart part. Honors the provided Content-Type of the uploaded file.

Parameters:
name - the request part name
dataFile - the file to use as the content body. You can get a handle to an uploaded file using test.getFile(String)
contentType - the Content-Type of the file being uploaded

makeMultiPart

void makeMultiPart()
Change the request into a multipart request, so that each named parameter is treated as separate part.

Multipart requests are made automatically after calling addFileUpload() so there is no need to call this function in that case.


Copyright © 2014 Neustar, Inc. All Rights Reserved.