Last active
August 9, 2023 03:36
-
-
Save peterjpxie/b6db7c8c1caadf7301421ae3eeb92fa8 to your computer and use it in GitHub Desktop.
Python Rest API sample - POST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
def test_post_headers_body_json(): | |
url = 'https://httpbin.org/post' | |
# Additional headers. | |
headers = {'Content-Type': 'application/json' } | |
# Body | |
payload = {'key1': 1, 'key2': 'value2'} | |
# convert dict to json string by json.dumps() for body data. | |
resp = requests.post(url, headers=headers, data=json.dumps(payload,indent=4)) | |
# Validate response headers and body contents, e.g. status code. | |
assert resp.status_code == 200 | |
resp_body = resp.json() | |
assert resp_body['url'] == url | |
# print response full body as text | |
print(resp.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really appreciate that. Updated.