Converting API request from curl to python

Bilal Mussa
Towards Dev
Published in
2 min readSep 15, 2022
Photo by Jon Cellier on Unsplash

I have recently created an app in streamlit using python to fetch the latest exchange rates from Remitly and Wise and then carry out some comparisons to see who’s the best provider. The initial challenge I faced was that the API documentation provided by Remitly had the information using curl. I do not know curl so I had to translate this into python. I used the following website to help me get started: https://curlconverter.com/

So the API using curl is like the below
curl -H “X-API-KEY: <your-key-here>” “https://corridor-package-info.remitly.com/v0/packages/USA-PHL"

Using python requests we need to first define the header paylod as the API Key will be sent in the header as part of the request

import requests headers = { ‘X-API-KEY’: ‘<your-key-here>’, } response = requests.get(‘https://corridor-package-info.remitly.com/v0/packages/USA-PHL', headers=headers)

Now if you had other things to add into the header like your business ID, the content/application you’d put it as part of the headers dictionary and then your get request will use that. So this is an example turning a curl request to python using headers.

What if you had to do basic auth in your get request?
For instance, you were given a username and password like this

Username = YourName
Password = YourPassword

In such cases, you’d use the auth option in the get request and embed your username and password like below

import requestsurl = ‘https://api.transferwise.com/v3/comparisons/?sourceCurrency='GBP'&targetCurrency='INR'&sendAmount='100'response = requests.get(url ,auth=(‘YourName’, ‘YourPassword’))

So thats a quick overview on how to convert curl to python requests or use the basic auth option if necessary depending on where you are pulling the data and how their setup is

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Written by Bilal Mussa

I enjoy finding solutions to business problems using data and data science. Keep it simple is my mantra.

No responses yet

Write a response