Workflows and String are shutting down on March 31, 2027.Learn more
HTTP requests are fundamental to working with APIs or other web services. You can make HTTP requests to retrieve data from APIs, fetch HTML from websites, or do pretty much anything your web browser can do.Below, we’ll review how to make HTTP requests using Python code on Pipedream.We recommend using the popular requests HTTP client package available in Python to send HTTP requests, but you can use any PyPI package you’d like on Pipedream.
No need to run pip install, just import requests at the top of your step’s code and it’s available for your code to use.To use requests on Pipedream, you’ll just need to import the requests PyPI package:
import requests
You make HTTP requests by passing a URL and optional request parameters to one of Requests’ 7 HTTP request methods.Here’s how to make a basic HTTP request on Pipedream:
r = requests.get('https://swapi.dev/api/films/')
The Response object r contains a lot of information about the response: its content, headers, and more. Typically, you just care about the content, which you can access in the text property of the response:
r = requests.get('https://swapi.dev/api/films/')# HTTP response content is in the text propertyr.text
Requests automatically decodes the content of the response based on its encoding, r.encoding, which is determined based on the HTTP headers.If you’re dealing with JSON data, you can call r.json() to decode the content as JSON:
r = requests.get('https://swapi.dev/api/films/')# The json-encoded content of a response, if anyr.json()
If JSON decoding fails, r.json() raises an exception.
GET requests typically are for retrieving data from an API. Below is an example.
import requestsdef handler(pd: "pipedream"): url = "https://swapi.dev/api/people/1" r = requests.get(url) # The response is logged in your Pipedream step results: print(r.text) # The response status code is logged in your Pipedream step results: print(r.status_code)
import requestsdef handler(pd: "pipedream"): # This a POST request to this URL will echo back whatever data we send to it url = "https://postman-echo.com/post" data = {"name": "Bulbasaur"} r = requests.post(url, data=data) # The response is logged in your Pipedream step results: print(r.text) # The response status code is logged in your Pipedream step results: print(r.status_code)
When you make a POST request, pass a dictionary with the data you’d like to send to the data argument. Requests automatically form-encodes the data when the request is made.
The code example above will NOT set the Content-Type header, meaning it will NOT be set to application/json.If you want the header set to application/json and don’t want to encode the dict yourself, you can pass it using the json parameter and it will be encoded automatically:
url = "https://postman-echo.com/post" data = {"name": "Bulbasaur"} r = requests.post(url, json=data)
Retrieve fake comment data on a specific post using JSONPlaceholder, a free mock API service. Here, you fetch data from the /comments resource, retrieving data for a specific post by query string parameter: /comments?postId=1.
import requestsdef handler(pd: "pipedream"): url = "https://jsonplaceholder.typicode.com/comments" params = {"postId": 1} # Make an HTTP GET request using requests r = requests.get(url, params=params) # Retrieve the content of the response data = r.text
You should pass query string parameters as a dictionary using the params keyword argument, like above. When you do, requests automatically URL-encodes the parameters for you, which you’d otherwise have to do manually.
To add HTTP headers to a request, pass a dictionary to the headers parameter:
import requestsimport jsondef handler(pd: "pipedream"): url = "https://jsonplaceholder.typicode.com/posts" headers = {"Content-Type": "application/json"} data = {"name": "Luke"} # Make an HTTP POST request using requests r = requests.post(url, headers=headers, data=json.dumps(data))
Most APIs require you authenticate HTTP requests with an API key or other token. Please review the docs for your service to understand how they accept this data.Here’s an example showing an API key passed in an HTTP header:
import requestsdef handler(pd: "pipedream"): url = "https://jsonplaceholder.typicode.com/posts" headers = {"X-API-KEY": "123"} # API KEY data = {"name": "Luke"} # Make an HTTP POST request using requests r = requests.post(url, headers=headers, json=data)
This example shows you how to download a file to a file in the /tmp directory. This can be especially helpful for downloading large files: it streams the file to disk, minimizing the memory the workflow uses when downloading the file.
import requestsdef handler(pd: "pipedream"): # Download the webpage HTML file to /tmp with requests.get("https://example.com", stream=True) as response: # Check if the request was successful response.raise_for_status() # Open the new file /tmp/file.html in binary write mode with open("/tmp/file.html", "wb") as file: for chunk in response.iter_content(chunk_size=8192): # Write the chunk to file file.write(chunk)
This example shows you how to make a multipart/form-data request with a file as a form part. You can store and read any files from the /tmp directory.This can be especially helpful for uploading large files: it streams the file from disk, minimizing the memory the workflow uses when uploading the file.
Using an HTTP proxy to proxy requests through another host
By default, HTTP requests made from Pipedream can come from a range of IP addresses. If you need to make requests from a single IP address, you can route traffic through an HTTP proxy:
import requestsdef handler(pd: "pipedream"): user = "USERNAME" # Replace with your HTTP proxy username password = "PASSWORD" # Replace with your HTTP proxy password host = "10.10.1.10" # Replace with the HTTP proxy URL port = 1080 # Replace with the port of the HTTP proxy proxies = { "https": f"http://{user}:{password}@{host}:{port}", } r = requests.request("GET", "https://example.com", proxies=proxies)
When you fetch data from an API, the API may return records in “pages”. For example, if you’re trying to fetch a list of 1,000 records, the API might return those in groups of 100 items.Different APIs paginate data in different ways. You’ll need to consult the docs of your API provider to see how they suggest you paginate through records.