How to make a Python 3.9 code 'requests.get' in Lambda.



I need to get the country information from IP address by GeoLite2 Country web service.

It is necessary to use 'curl' command as following. curl -u "{account_id}:{license_key}" "{ip_address}?pretty"

*reference URL

But python doesn't have 'curl' command, so I need to use 'requests.get' command. I tested the code as following. ( is the IP address in Japan.)


import requests import os

def lambda_handler(event, context): response = requests.get('', auth=('account_id', 'license_key')) return response.json()

<<This is the result when I run code1.>>

{ "continent": { "code": "AS", "geoname_id": 6255147, "names": { "zh-CN": "亚洲", "de": "Asien", "en": "Asia", "es": "Asia", "fr": "Asie", "ja": "アジア", "pt-BR": "Ásia", "ru": "Азия" } }, "country": { "iso_code": "JP", "geoname_id": 1861060, "names": { "ja": "日本", "pt-BR": "Japão", "ru": "Япония", "zh-CN": "日本", "de": "Japan", "en": "Japan", "es": "Japón", "fr": "Japon" } }, "registered_country": { "iso_code": "US", "geoname_id": 6252001, "names": { "ru": "США", "zh-CN": "美国", "de": "Vereinigte Staaten", "en": "United States", "es": "Estados Unidos", "fr": "États Unis", "ja": "アメリカ", "pt-BR": "EUA" } }, "traits": { "ip_address": "", "network": "" } }

If I use variable 'url' to the argument of 'requests.get',


import requests import os

def lambda_handler(event, context): url = "'', auth=('account_id', 'license_key')" response = requests.get(url)

<<result of code2 (erroe) >>

"errorMessage": "No connection adapters were found for "'', auth=('account_id', 'license_key')"", "errorType": "InvalidSchema",

<<code3: This works well>>

url = "" response = requests.get(url)

Why doesn't code2 work??


asked 7 months ago49 views
2 Answers
Accepted Answer

I solved this problem.

I found that this API used Basic authentication. Username is 'account_id', password is 'license_key'. So I use 'requests.auth.HTTPBasicAuth'.

This is the code.

url1 = ""
ip = os.environ['ip']
url2 ="?pretty"
url3 = url1 + ip +url2
account_id = os.environ['account_id']
license_key = os.environ['license_key']

r = requests.get(url3, auth=requests.auth.HTTPBasicAuth(account_id, license_key))

country = r.json()["country"]["names"]["ja"]
answered 7 months ago

re:Post has messed up your formatting so it's a bit hard to understand, but from what I can see your "code1" is calling requests.get() like this:

requests.get(String, auth=('account_id', 'license_key'))

whereas your "code2" is calling it like this:


So they are quite different.

answered 7 months ago
  • I'm sorry that the format is broken and it's hard to understand.

    <<code1>> response = requests.get('', auth=('account_id', 'license_key')) return response.json()

        url = "'', auth=('account_id', 'license_key')"
        response = requests.get(url)
        return response.json()


You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions