Skip to content

Instantly share code, notes, and snippets.

@ojarva
Created April 20, 2018 07:14
Show Gist options
  • Save ojarva/3d1ffc71e9570e7d824eaedca6896a00 to your computer and use it in GitHub Desktop.
Save ojarva/3d1ffc71e9570e7d824eaedca6896a00 to your computer and use it in GitHub Desktop.
import requests
class RequestsTest(requests.Session):
def __init__(self):
super().__init__()
self.headers["accept"] = "application/json"
self.params["your_api_key_here"] = "my_api_key"
def get_with_params(self, params):
print(self.get("https://httpbin.org/get", params=params).text)
def get_with_params_fixed(self, params):
if isinstance(params, list):
for k, v in self.params.items():
params.append((k, v))
print(self.get("https://httpbin.org/get", params=params).text)
a = RequestsTest()
print("Send a dict of params")
a.get_with_params({"foo": 1}) # Works fine
print("Send a list of params")
a.get_with_params([("foo", 1), ("foo", 2)]) # Does not send api key
print("Send a list of params with fixed version")
a.get_with_params_fixed([("foo", 1), ("foo", 2)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment