Skip to content

Commit

Permalink
Added form-data request script
Browse files Browse the repository at this point in the history
  • Loading branch information
vdespa committed Feb 4, 2020
1 parent 73c462f commit ba7b2f9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
46 changes: 37 additions & 9 deletions docs/cheatsheet.rst
Original file line number Diff line number Diff line change
Expand Up @@ -332,26 +332,54 @@ this is the object containing the script that is running, can access variables a
pm.sendRequest
--------------

Allows to send simple HTTP(S) GET requests from tests and pre-request scripts. Example: ::
Allows to send **simple HTTP(S) GET requests** from tests and pre-request scripts. Example: ::

pm.sendRequest('http://example.com', function (err, res) {
console.log(err ? err : res.json());
pm.sendRequest('https://httpbin.org/get', (error, response) => {
if (error) throw new Error(error);
console.log(response.json());
});

Full-option HTTP(S) request: ::

const postRequest = {
url: 'http://example.com', method: 'POST',
Full-option **HTTP POST request with JSON body**: ::

const payload = { name: 'John', age: 29};
const options = {
method: 'POST',
url: 'https://httpbin.org/post',
header: 'X-Foo:foo',
body: {
mode: 'raw',
raw: JSON.stringify({ name: 'John' })
raw: JSON.stringify(payload)
}
};
pm.sendRequest(postRequest, function (err, res) {
console.log(err ? err : res.json());
pm.sendRequest(options, (error, response) => {
if (error) throw new Error(error);
console.log(response.json());
});

**Form-data POST request** (Postman will add the multipart/form-data header): ::

const options = {
'method': 'POST',
'url': 'https://httpbin.org/post',
'body': {
'mode': 'formdata',
'formdata': [
{'key':'foo', 'value':'bar'},
{'key':'bar', 'value':'foo'}
]
}
};
pm.sendRequest(options, (error, response) => {
if (error) throw new Error(error);
console.log(response.json());
});

**Sending a file with form-data POST request**

Due to security precautions, it is not possible to upload a file from a script using pm.sendRequest. You cannot read or write files from scripts.


Postman Echo
============
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# The short X.Y version
version = u''
# The full version, including alpha/beta/rc tags
release = u'Version 1.5.1 - December 2019'
release = u'Version 1.6.0 - February 2020'


# -- General configuration ---------------------------------------------------
Expand Down

0 comments on commit ba7b2f9

Please sign in to comment.