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
ConstructorsConstructorDescriptionConvenience 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/srcproperty, the path to the fileReturns an ArrayList<Property> containing formdata paramters:getFormdata(int position) Returns aPropertycontaining formdata property at the specified position in the arrayReturn an HashMap<String, String> containing the GraphQL query and variables source code.getKey()getMode()Returns themodeproperty as an enumerated valuegetRaw()Returns the content of therawproperty of the body, or null if it has not been setReturns the enumerated value of thelanguageproperty of theoptionspropertyvoidremoveFormData(int position) Removes the formdata element at the specified position in the formdata arrayvoidremoveFormData(Property data) Removes the formdata element at the specified position in the formdata arrayvoidsetBinarySrc(String file) Sets the value of thefile/srcproperty, the path to the filevoidsetFormdata(Property data) Sets an element of the formdata or urlencoded property arrayvoidsetFormdata(PropertyList<Property> data) Convenience method to set the formdata with an already filled ArrayList<Property> of propertiesvoidsetFormdata(String key, String value, String description) Add a formdata element by string valuesvoidsetGraphql(String graphQL) Sets the query portion of thegraphqlpropertyvoidsetGraphql(String graphQL, String variables) Sets both thequeryandvariablesproperties of thegraphqlelementvoidsetMode(enumRequestBodyMode newMode) Sets the value of themodeproperty and initializes relevant internal propertiesvoidSets therawproperty of the body, or null if it has not been setvoidsetRaw(String raw, enumRawBodyLanguage language) Sets the content (raw) and thelanguageproperty of theoptionsobjectvoidSets thelangaugeproperty of theoptionspropertyMethods 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. Ifmodeis not raw, the language value is discarded.- Parameters:
mode- Enumerated value for the underyingmodeproperty of this request bodycontent- The content in the bodylanguage- For bodies withmodeRAW, 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 themodeproperty
-
-
Method Details
-
getKey
- Specified by:
getKeyin classCollectionElement- Returns:
- String
-
getRaw
Returns the content of therawproperty of the body, or null if it has not been set- Returns:
- String The raw content
- Throws:
IllegalPropertyAccessException- If bodymodeis not RAW
-
setRaw
Sets therawproperty of the body, or null if it has not been set- Parameters:
raw-- Throws:
IllegalPropertyAccessException- If bodymodeis not RAW
-
setRawLanguage
Sets thelangaugeproperty of theoptionsproperty- Parameters:
lang-- Throws:
IllegalPropertyAccessException- Ifmodeis not RAW
-
getRawLanguage
Returns the enumerated value of thelanguageproperty of theoptionsproperty- Returns:
- enumRawBodyLanguage
- Throws:
IllegalPropertyAccessException- Ifmodeis not RAW
-
setRaw
Sets the content (raw) and thelanguageproperty of theoptionsobject- Parameters:
raw- The content of the body (raw)language- The language of the content, eg.javascript- Throws:
IllegalPropertyAccessException- Ifmodeis 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:
queryThe graphql queryvariablesThe variables, or null if none are configured - Throws:
IllegalPropertyAccessException- Ifmodeis not GRAPHQL
-
setGraphql
Sets the query portion of thegraphqlproperty- Parameters:
graphQL- The query part of the GraphQL property
-
setGraphql
Sets both thequeryandvariablesproperties of thegraphqlelement- Parameters:
graphQL- Thequeryproperty of thegraphqlelementvariables- Thevariablesproperty of thegraphqlelement
-
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- Ifmodeis not URLENCODED or FORMDATA
-
getFormdata
Returns aPropertycontaining formdata property at the specified position in the array- Parameters:
position- The position in the array- Returns:
- Property The form data.
- Throws:
IllegalPropertyAccessException- ifmodeis 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- Ifmodeis 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 thekeyproperty of the formdata elementvalue- The value for thevalueproperty of the formdata elementdescription- The value- Throws:
IllegalPropertyAccessException- Ifmodeis not URLENCODED or FORMDATA
-
setFormdata
Sets an element of the formdata or urlencoded property array- Parameters:
data- Populated Property containing the formdata- Throws:
IllegalPropertyAccessException- Ifmodeis not URLENCODED or FORMDATA
-
getFile
Returns the value of thefile/srcproperty, the path to the file- Returns:
- String The stored file spec
- Throws:
IllegalPropertyAccessException- Ifmodeis not FILE
-
setBinarySrc
Sets the value of thefile/srcproperty, the path to the file- Parameters:
file- The file path or name
-
getMode
Returns themodeproperty as an enumerated value- Returns:
- enumRequestBodyMode
-
setMode
Sets the value of themodeproperty 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- Ifmodeis 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- Ifmodeis not FORMDATA or URLENCODED
-