requests
HTTP client package available in Python to send HTTP requests, butyou can use any PyPi package you’d like on Pipedream.requests
usage notespip install
, justimport requests
at the top of your step’s code and it’s available for your code to use.To userequests
on Pipedream, you’ll just need to import therequests
PyPi package:import requests
r= requests.get('https://swapi.dev/api/films/')
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 thetext
property of the response:r= requests.get('https://swapi.dev/api/films/')# HTTP response content is in the text propertyr.text
r.encoding
, which is determined based on the HTTP headers.If you’re dealing with JSON data, you can callr.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()
r.json()
raises an exception.GET
requestimport 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)
POST
requestimport 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)
POST
request, pass a dictionary with the data you’d like to send to thedata
argument. Requests automatically form-encodes the data when the request is made.Content-Type
header, meaning it will NOT be set toapplication/json
.If you want the header set toapplication/json
and don’t want to encode thedict
yourself, you can pass it using thejson
parameter and it will be encoded automatically: url= "https://postman-echo.com/post" data = {"name":"Bulbasaur"} r= requests.post(url, json=data)
GET
request/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
params
keyword argument, like above. When you do,requests
automaticallyURL-encodes the parameters for you, which you’d otherwise have to do manually.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))
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)
/tmp
directory:import requestsdef handler(pd:"pipedream"): # Retrieving a previously saved file from workflow storage files= {"image":open("/tmp/python-logo.png","rb")} r= requests.post(url="https://api.imgur.com/3/image",files=files)
/tmp
directory/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 chunkin response.iter_content(chunk_size=8192): # Write the chunk to file file.write(chunk)
/tmp
directorymultipart/form-data
request with a file as a form part. You can store and read any files fromthe/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.import requestsfrom requests_toolbelt.multipart.encoderimport MultipartEncoderdef handler(pd:"pipedream"): m= MultipartEncoder(fields={ 'file': ('filename',open('/tmp/file.pdf','rb'),'application/pdf') }) r= requests.post("https://example.com",data=m, headers={'Content-Type': m.content_type})
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)
import requestsdef handler(pd:"pipedream"): url= "https://beta.pokeapi.co/graphql/v1beta" query= """query samplePokeAPIquery { generations: pokemon_v2_generation { name pokemon_species: pokemon_v2_pokemonspecies_aggregate { aggregate { count } } }} """ r= requests.post(url,json={"query": query}) return r.json()
pd.inputs[appName]["$auth"]
:import requestsdef handler(pd:"pipedream"): url= "https://api.github.com/graphql" query= """query { viewer { login }} """ token= pd.inputs["github"]["$auth"]["oauth_access_token"] headers= {"authorization":f"Bearer{token}"} r= requests.post(url,json={"query": query},headers=headers) return r.json()
Was this page helpful?