Skip to content

Instantly share code, notes, and snippets.

@peterjpxie
Last active August 9, 2023 03:36
Show Gist options
  • Save peterjpxie/b6db7c8c1caadf7301421ae3eeb92fa8 to your computer and use it in GitHub Desktop.
Save peterjpxie/b6db7c8c1caadf7301421ae3eeb92fa8 to your computer and use it in GitHub Desktop.
Python Rest API sample - POST
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)
@peterjpxie
Copy link
Author

headers variable is not used

Really appreciate that. Updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment