Postman cheat sheet

Postman has been my goto tool for API testing for the past year or so. When I hit a wall with using it I turn to All Things Postman. But I realized most of what I end up looking for is the basic stuff that covers 90 percent of my daily use in one place.

Environment Variables

pm.environment.set('someVariable', someValue)
pm.environment.get('someVariable')
pm.environment.unset('someVariable')

Lodash

//select random item from an array
_.sample([a, b, c])

//random number within a range
_.random(1, 10)

//repeat a command
_.times(5, () => {
    //do stuff
})

Moment

const now = moment()
const tomorrow = moment().add(1, 'day')

JWT Token

const jwt = jsonData.access_token.split('.')
console.log(JSON.parse(atob(jwt[1])))

Tests

pm.test('example test', () => {
    pm.expect(pm.response.code).to.eql(200)
    pm.expect(pm.response.status).to.eql('OK')

    const response = pm.resonse.json()

    if response.length === null throw new ERROR(    )

    //verify response value
    pm.expect(response.name).to.eql('Marc')

    //verify a date is a valid date
    pm.expect(moment(response.date).isValid()).to.be.true

    //verify result matches a regex
    pm.expect(response.name).to.match(/[A-Z}{1}[a-z]{3}/)

    //verify type of object
    pm.expect(response.name).to.be.a('string')

    //verify a header value
    pm.expect(postman.getResponseHeader('some-header'))
        .to.eql('some-value')
})

Scripts

Send a post request

pm.sendRequest(
  {
    url: 'http://testurl',
    method: 'POST',
    header: {
      'Content-Type': 'application/json'
    },
    body: {
      mode: 'raw',
      raw: JSON.stringify(someJSON)
    }
  },
  (err, res) => {
    console.log(err ? err : res.json())
  }
)

Set next request (Note that this uses postman object and not pm obect)

postman.setNextRequest('name of request')

Newman

//run a collection
newman run my_collection.postman_collection.json

//run with environment file
newman run my_collection.postman_collection.json
-e my_environment.postman_environment.json

//run with data file
newman run my_collection.postman_collection.jsonn -d data.json

//disable SSL validations (useful if running through a proxy)
newman run my_collection.postman_collection.json --insecure

No Comments Yet