How To Send Rest API Put Request Using Python?

Timothy Joseph | April 11, 2022

How To Send Rest API Put Request Using Python?

API testing is widely used in any professional automation framework. API allows different software components to interact with each other. HTTP is one of the methods to make HTTP calls. PUT is one of the HTTP methods which is used when the user wants to UPDATE data on the server-side.

For automaton of PUT scenario, below steps are commonly used:

  • Step 1: Read data from JSON File and parse into JSON format
  • Step 2: Execute your Put Action
  • Step 3: Parse Response in JSON Format and validate
  • Step 4: Assert Response

Additionally, for making HTTP PUT request, below components are required to make a successful request:

  • Request URL: Server URL where data needs to be updated.
  • Request Headers: it contains Content-Type & Authorization Token
  • Request Body: Data which will be sent with request

This can be implemented in automation script as a reusable method in following way:

@staticmethod
def send_PUT_Request(statusCode,payload,header):
response=requests.put(<baseUrl>+<endPoint>,headers=header,json=payload)
data = dump.dump_all(response)
print("\n\n--------------Request--------------------------\n",data.decode('utf-8'))
assert response.status_code==statusCode
if(response.status_code!=statuscode.STATUS_NO_CONTENT):
dataValue=response.json()
return dataValue

Disclaimer

This publication is for informational purposes only, and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.

Post a Comment