Software Development and QA Tips By QASource Experts

How To Send Rest API Put Request Using Python?

Written by Timothy Joseph | Apr 11, 2022 4:00:00 PM

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