Package com.postman.collection
Class BodyElement
java.lang.Object
com.postman.collection.CollectionElement
com.postman.collection.BodyElement
Encapsulates the body
property of a Request
object.
Postman SDK analog: RequestBody
There are several different permutations for this property depending on the
mode
selected in Postman. Some examples:
plaintext:
"body": { "mode": "raw", "raw": "This is some text in the body" }Form data Formdata and Urlencoded bodies comprise an array of key value pairs persisted as instance of Property:
"body": { "mode": "urlencoded", "urlencoded": [ { "key": "x-field-1", "value": "x-field-1 value", "description": "This is x-field-1", "type": "text" }, { "key": "x-field-2", "value": "x-field-2 value", "description": "This is x-field-2", "type": "text" }, { "key": "x-field-3", "value": "{{Xfield3value}}", "description": "variable", "type": "text" } ] }RAW options
Other forms of textual data contain an options
property which only includes the langauge of the payload, e.g., javascript
"body": { "mode": "raw", "raw": "pm.test(\"Status code is 200\", function () {\n ...", "options": { "raw": { "language": "javascript" } } }
GraphQL
GraphQL includes query and variable properties
"body": { "mode": "graphql", "graphql": { "query": "query ($limit: Int!) {\n ...", "variables": "{\n \"limit\":2\n}" } }
Binary
Binary bodies contain a single src
property with a path to the file
"body": { "mode": "file", "file": { "src": "8vhckkNqZ/jenkins-small.png" } }
-
Constructor Summary
ConstructorDescriptionConvenience constructor, creates an empty body object of the specified modeBodyElement
(enumRequestBodyMode mode, String content, enumRawBodyLanguage language) Constructs a body with the providedmode
, content, and language. -
Method Summary
Modifier and TypeMethodDescriptiongetFile()
Returns the value of thefile/src
property, the path to the fileReturns an ArrayList<Property> containing formdata paramters:getFormdata
(int position) Returns aProperty
containing formdata property at the specified position in the arrayReturn an HashMap<String, String> containing the GraphQL query and variables source code.getKey()
getMode()
Returns themode
property as an enumerated valuegetRaw()
Returns the content of theraw
property of the body, or null if it has not been setReturns the enumerated value of thelanguage
property of theoptions
propertyvoid
removeFormData
(int position) Removes the formdata element at the specified position in the formdata arrayvoid
removeFormData
(Property data) Removes the formdata element at the specified position in the formdata arrayvoid
setBinarySrc
(String file) Sets the value of thefile/src
property, the path to the filevoid
setFormdata
(Property data) Sets an element of the formdata or urlencoded property arrayvoid
setFormdata
(PropertyList<Property> data) Convenience method to set the formdata with an already filled ArrayList<Property
> of propertiesvoid
setFormdata
(String key, String value, String description) Add a formdata element by string valuesvoid
setGraphql
(String graphQL) Sets the query portion of thegraphql
propertyvoid
setGraphql
(String graphQL, String variables) Sets both thequery
andvariables
properties of thegraphql
elementvoid
setMode
(enumRequestBodyMode newMode) Sets the value of themode
property and initializes relevant internal propertiesvoid
Sets theraw
property of the body, or null if it has not been setvoid
setRaw
(String raw, enumRawBodyLanguage language) Sets the content (raw
) and thelanguage
property of theoptions
objectvoid
Sets thelangauge
property of theoptions
propertyMethods inherited from class com.postman.collection.CollectionElement
getCollection, getDefaultCollectionSchema, getDefaultValidationSchema, getParent, getUUID, getValidationMessages, isEquivalentTo, setParent, setUUID, toJson, toJson, validate, validate
-
Constructor Details
-
BodyElement
Constructs a body with the providedmode
, content, and language. Ifmode
is not raw, the language value is discarded.- Parameters:
mode
- Enumerated value for the underyingmode
property of this request bodycontent
- The content in the bodylanguage
- For bodies withmode
RAW, the language of the body content, e.g.,javascript
-
BodyElement
Convenience constructor, creates an empty body object of the specified mode- Parameters:
mode
- Enumerated value for themode
property
-
-
Method Details
-
getKey
- Specified by:
getKey
in classCollectionElement
- Returns:
- String
-
getRaw
Returns the content of theraw
property of the body, or null if it has not been set- Returns:
- String The raw content
- Throws:
IllegalPropertyAccessException
- If bodymode
is not RAW
-
setRaw
Sets theraw
property of the body, or null if it has not been set- Parameters:
raw
-- Throws:
IllegalPropertyAccessException
- If bodymode
is not RAW
-
setRawLanguage
Sets thelangauge
property of theoptions
property- Parameters:
lang
-- Throws:
IllegalPropertyAccessException
- Ifmode
is not RAW
-
getRawLanguage
Returns the enumerated value of thelanguage
property of theoptions
property- Returns:
- enumRawBodyLanguage
- Throws:
IllegalPropertyAccessException
- Ifmode
is not RAW
-
setRaw
Sets the content (raw
) and thelanguage
property of theoptions
object- Parameters:
raw
- The content of the body (raw
)language
- The language of the content, eg.javascript
- Throws:
IllegalPropertyAccessException
- Ifmode
is not RAW
-
getGraphql
Return an HashMap<String, String> containing the GraphQL query and variables source code. Either or both may be null if they don't exist.- Returns:
- HashMap<String,String> A HashMap with the following keys:
query
The graphql queryvariables
The variables, or null if none are configured - Throws:
IllegalPropertyAccessException
- Ifmode
is not GRAPHQL
-
setGraphql
Sets the query portion of thegraphql
property- Parameters:
graphQL
- The query part of the GraphQL property
-
setGraphql
Sets both thequery
andvariables
properties of thegraphql
element- Parameters:
graphQL
- Thequery
property of thegraphql
elementvariables
- Thevariables
property of thegraphql
element
-
getFormdata
Returns an ArrayList<Property> containing formdata paramters:{ "key": "FormField1", "value": "Field 1 value", "description": "This is for Field 1", "type": "text" }, { "key": "FormField2", "value": "Field 2 value", "description": "This is for Field 2", "type": "text" }
- Returns:
- ArrayList<
Property
> The data - Throws:
IllegalPropertyAccessException
- Ifmode
is not URLENCODED or FORMDATA
-
getFormdata
Returns aProperty
containing formdata property at the specified position in the array- Parameters:
position
- The position in the array- Returns:
- Property The form data.
- Throws:
IllegalPropertyAccessException
- ifmode
is not URLENCODED or FORMDATA
-
setFormdata
Convenience method to set the formdata with an already filled ArrayList<Property
> of properties- Parameters:
data
- The filled ArrayList<Property
>- Throws:
IllegalPropertyAccessException
- Ifmode
is not URLENCODED or FORMDATA
-
setFormdata
public void setFormdata(String key, String value, String description) throws IllegalPropertyAccessException Add a formdata element by string values- Parameters:
key
- The value for thekey
property of the formdata elementvalue
- The value for thevalue
property of the formdata elementdescription
- The value- Throws:
IllegalPropertyAccessException
- Ifmode
is not URLENCODED or FORMDATA
-
setFormdata
Sets an element of the formdata or urlencoded property array- Parameters:
data
- Populated Property containing the formdata- Throws:
IllegalPropertyAccessException
- Ifmode
is not URLENCODED or FORMDATA
-
getFile
Returns the value of thefile/src
property, the path to the file- Returns:
- String The stored file spec
- Throws:
IllegalPropertyAccessException
- Ifmode
is not FILE
-
setBinarySrc
Sets the value of thefile/src
property, the path to the file- Parameters:
file
- The file path or name
-
getMode
Returns themode
property as an enumerated value- Returns:
- enumRequestBodyMode
-
setMode
Sets the value of themode
property and initializes relevant internal properties- Parameters:
newMode
-
-
removeFormData
Removes the formdata element at the specified position in the formdata array- Parameters:
position
- The position in the array of formdata elements to remove. This value is not bounds checked, so an ArrayOutOfBounds exception may occur.- Throws:
IllegalPropertyAccessException
- Ifmode
is not FORMDATA or URLENCODED
-
removeFormData
Removes the formdata element at the specified position in the formdata array- Parameters:
data
- The Property to remove.- Throws:
IllegalPropertyAccessException
- Ifmode
is not FORMDATA or URLENCODED
-