Last active
January 29, 2023 21:39
-
-
Save RafalSkolasinski/74845431c62d25915a05d9ff93fa71e6 to your computer and use it in GitHub Desktop.
Send some simple REST requests with click CLI
This file contains hidden or 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 click | |
import requests | |
import json | |
import gzip | |
@click.group() | |
def cli(): | |
pass | |
@cli.command() | |
@click.option("--host", "-h", default="localhost") | |
@click.option("--port", "-p", default=5000, type=int) | |
def get_normal(host, port): | |
r = requests.get( | |
url=f"http://{host}:{port}/api/get/encoded", | |
headers={"Accept-Encoding": "deflate"}, | |
) | |
print("response status:", r.status_code) | |
print("response text:", r.text) | |
print("response headers:", r.headers) | |
@cli.command() | |
@click.option("--host", "-h", default="localhost") | |
@click.option("--port", "-p", default=5000, type=int) | |
def get_encoded(host, port): | |
r = requests.get( | |
url=f"http://{host}:{port}/api/get/encoded", | |
headers={"Accept-Encoding": "gzip, deflate"}, | |
) | |
print("response status:", r.status_code) | |
print("response text:", r.text) | |
print("response headers:", r.headers) | |
@cli.command() | |
@click.option("--host", "-h", default="localhost") | |
@click.option("--port", "-p", default=5000, type=int) | |
def post_normal(host, port): | |
r = requests.post( | |
url=f"http://{host}:{port}/api/post/encoded", | |
data=json.dumps({"not-compressed": "request"}), | |
headers={"Content-Type": "application/json"}, | |
) | |
print("response status:", r.status_code) | |
print("response text:", r.text) | |
print("response headers:", r.headers) | |
@cli.command() | |
@click.option("--host", "-h", default="localhost") | |
@click.option("--port", "-p", default=5000, type=int) | |
def post_encoded(host, port): | |
r = requests.post( | |
url=f"http://{host}:{port}/api/post/encoded", | |
data=gzip.compress(json.dumps({"compressed": "request"}).encode("utf8"), 5), | |
headers={"Content-Encoding": "gzip", "Content-Type": "application/json"}, | |
) | |
print("response status:", r.status_code) | |
print("response text:", r.text) | |
print("response headers:", r.headers) | |
if __name__ == "__main__": | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment