DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1

payload that sends some random text to the service. The
response to the request is stored in a response object
called response. Everything in the response can be
parsed, and further actions can be taken. This example
simply prints the values of a few attributes of the
response.


Example 7-13 Simple HTTP POST Using Python
Requests


Click here to view code image


import requests
url = "https://postman-echo.com/post"
payload = "hello DevNet"
headers = {'content-type': "text/plain"}
response = requests.request("POST", url,
data=payload, headers=headers)
print(response.text)

Example 7-14 shows a simple Python script that uses the
Requests library and does a GET request to the Postman
Echo server. One difference you will notice between
Example 7-11 and Example 7-14 is related to
authentication. With the Requests library, authentication
is usually done by passing the ‘authorization’ keyword
along with the type and key.


Example 7-14 Basic Auth Using Python Requests

Click here to view code image


import requests
url = "https://postman-echo.com/basic-auth"
headers = {
'authorization': "Basic
cG9zdG1hbjpwYXNzd29yZA=="
}
response = requests.request("GET", url,
headers=headers)
print(response.text)

REST API Debugging Tools for Developing APIs

Free download pdf