Karate API: Using Content-Type as x-www-form-urlencoded

Many at times we need to use different content types , headers , parameters while working with different API testing.
Today i have figured out a way how to send a different content type as x-www-form-urlencoded for a POST request.

For one my project we have to generate access token using content type as x-www-form-urlencoded and I found it difficult to send it from rest assured or Karate code. However it was easy to send the same request using POSTMAN.

Finally after a lot of trial and error and search over google I found a solution.

USE CASE:
1- Make a POST request
2- Set contet-type as x-www-form-urlencoded in Body (Hence there will be no body content for this content type, only it will have related form parameters)
3- Set 3 parameters for the above content-type
– grant_type : password
– username: abcd
– password: abcd

Sample request in POSTMAN

Now I have done the above request using KARATE API as below.

Feature: To test Authentication API using x-www-form-urlencoded
Scenario: Generate access token 
	Given url 'https://login-ppr.schneider-electric.com/api/v1/token'
	And header Content-Type = 'application/x-www-form-urlencoded'
	And form field username = '[email protected]'
	And form field password = '[email protected]'
	And form field grant_type = 'password'
	And request {}
 	When method post
 	Then status 200 

Output:
1 < 200
{"access_token":"5bb73fc5-6e69-43b6-931d-eb80c87a2cc9","token_type":"bearer","expires_in":1799}

Leave a Reply