pythonpython-requestsopenstack-python-api

Failure to add params to endpoint


Hello guys while trying to make a simple script i have encountered an error and i've scratched google's head and mine but didn't find a solution . So the problem is anytime i run this code i get a response from the server saying ' the api-key is missing' instead of giving me info on the number i enter i dont know if i'm doing anything wrong btw . Any help would you be appreciated This is a sample of my code

import requests
list = input('Input Phone Numbers List :')
link = "http://apilayer.net/api/validate"
head = {'User-agent': 'user-agent-here'}
s = requests.session()
session = s.get(link,headers=head)
phone = open(list, 'r')
while True:
    num = phone.readline().replace('\n', '')
    if not num:
        break
    cot = num.strip().split(':')
    send = s.post(link,
    data={'access_key':'1135810505585d6e034f640fbf30a700','number':cot[0]},headers=head,)
    (stats, respond) = (send.status_code, send.text)
    print (stats, respond)

Solution

  • Example on numverify.com shows that it needs GET request so it needs values as get(..., params=...) but at start (before while True) you use get() without any parameters - and it makes problem.

    You don't need post() and (as in most API) you don't need headers, and cookies.

    import requests
    
    #list = input('Input Phone Numbers List :')
    
    link = "http://apilayer.net/api/validate"
    
    payload = {
        'access_key': '1135810505585d6e034f640fbf30a700',
        'number': '',
    }
    
    #phone = open(list, 'r')
    phone = ['+14158586273', '+46123456789']
    
    for num in phone:
        num = num.strip()
        if num:
            cot = num.split(':')
            
            payload['number'] = cot[0]
            
            response = requests.get(link, params=payload)
            
            print('status:', response.status_code)
            print('text:', response.text)
            print('---')
            
            data = response.json()
            
            print('number:', data['international_format'])
            print('country:', data['country_name'])
            print('location:', data['location'])
            print('carrier:', data['carrier'])
            print('---')
            
            
            
    

    Result:

    status: 200
    text: {"valid":true,"number":"14158586273","local_format":"4158586273","international_format":"+14158586273","country_prefix":"+1","country_code":"US","country_name":"United States of America","location":"Novato","carrier":"AT&T Mobility LLC","line_type":"mobile"}
    ---
    number: +14158586273
    country: United States of America
    location: Novato
    carrier: AT&T Mobility LLC
    ---
    status: 200
    text: {"valid":true,"number":"46123456789","local_format":"0123456789","international_format":"+46123456789","country_prefix":"+46","country_code":"SE","country_name":"Sweden","location":"Valdemarsvik","carrier":"","line_type":"landline"}
    ---
    number: +46123456789
    country: Sweden
    location: Valdemarsvik
    carrier: 
    ---