top of page

Pre-request script and Test script in Postman

  • Writer: everyone testing4
    everyone testing4
  • Jun 27, 2022
  • 1 min read

Example - Pre-request:

Example: Body request as formdata



const base_url = pm.variables.get('base_url')

const getTokenRequest = {
  url: base_url + '/api/login',
  method: 'POST',
  body: {
      mode: 'formdata',
      formdata: [
          { key: 'username', value: 'will' },
          { key: 'password', value: '1234' }
      ],
    }
};

pm.sendRequest(getTokenRequest, (err, response) => {
  const jsonResponse = response.json();
  const newAccessToken = jsonResponse.access_token;

  pm.variables.set('accessToken', newAccessToken);
});


Example: body request as Json



const getTokenRequest={
	url: 'https://<my url>.auth0.com/oauth/token',
	method: 'POST',
	header: 'Content-Type:application/json',
	body: {
		mode: 'application/json',
		raw: JSON.stringify(
			{client_id:'<your client ID>',
			client_secret:'<your client secret>',
			audience:'<my audience>',
			grant_type:'client_credentials'
		})
	}
};
pm.sendRequest(getTokenRequest, (err, response) => {
  const jsonResponse = response.json();
  const newAccessToken = jsonResponse.access_token;

  pm.variables.set('accessToken', newAccessToken);
});

Example - Test script:


pm.test("check Status response and get Token", function(){
    pm.expect(pm.response.status == 201);
    var responseData = pm.response.json();
    pm.environment.set("accessToken", responseData.access_token);
});

 
 
 

Comments


logo.png

Hi, We're Testing4Everyone

We are building up QA/QC community to share technical skills about software testing to everyone. 

  • Facebook
  • Twitter
  • LinkedIn
  • Instagram

Software Testing:

+ Testing concepts

+ Manual testing tips and skills

+ Automation test for Web/ Mobile / Win App.
+ Performance testing for Web/Mobile app.
+ Security testing for QA and QC.

Subscribe

Thanks for submitting!

©2023 by Jeff Sherman. Proudly created with Wix.com

bottom of page