Home > Net >  python requests invalid parameter
python requests invalid parameter

Time:12-01

I'm trying to post a request to this API. Here is my code:

import requests
accesstoken = "74e41f9c-8ae6-4ebd-9568-f0c92e83bb54"
authnn = "4ef2db2b-70ea-11ed-9f86-063d0d6fdfb5"

def sender(number):
    smstext = "hello test now#" 
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0','accessToken': accesstoken,'Authorization': authnn}
    json_data = {
        'content': smstext,
        'contentType': 'TEXT',
        'from': ' 447860002234',
        'to': number,
    }
    response = requests.post('https://api-sandbox.exmpl.io/v1/mms/', headers=headers, json=json_data)
    resp =(response.text)
    if '"acceptedTime":"' in resp:
        print("SENT OK | {} | {}" .format(number,resp))
        print(json_data)
        time.sleep(10)
    else:
        print("ERROR => {}".format(resp))
        print(json_data)
        exit()
if __name__ == "__main__":
    nums = input("NUMBERS LIST  : ")
    op = open(nums, "r")
    for i in op:
     for i in i.split():
         sender(i)

I get error

ERROR => {"code":"***","message":"Invalid parameter -  : to"}

I printed the JSON to me I see no error :

{'content': 'hello test now#', 'contentType': 'TEXT', 'from': ' 447860002234', 'to': ' 4113322422787'}

When I do a POST request like this with preloaded data it works fine.

import requests
accesstoken = "74e41f9c-8ae6-4ebd-9568-f0c92e83bb54"
authnn = "4ef2db2b-70ea-11ed-9f86-063d0d6fdfb5"

def sender(number):
    smstext = "hello bebe test hada wewe hehe dede nene tete Ref#" 
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0','accessToken': accesstoken,'Authorization': authnn}
    json_data = {
        'content': smstext,
        'contentType': 'TEXT',
        'from': ' 4478623212234',
        'to': ' 4113322422787',
    }
    response = requests.post('https://api-sandbox.exmpl.io/v1/mms/', headers=headers, json=json_data)
    resp =(response.text)
    if '"acceptedTime":"' in resp:
        print("SENT OK | {} | {}" .format(number,resp))
        print(json_data)
        time.sleep(10)
    else:
        print("ERROR => {}".format(resp))
        print(json_data)
        exit()
if __name__ == "__main__":
    nums = input("NUMBERS LIST  : ")
    op = open(nums, "r")
    for i in op:
     for i in i.split():
         sender(i)

I'm expecting to load value in JSON with a for loop.

CodePudding user response:

I suspect your problem lies with this block of code:

if __name__ == "__main__":
    nums = input("NUMBERS LIST  : ")
    op = open(nums, "r")
    for i in op:
     for i in i.split():
         sender(i)

In that you are probably not calling the sender function with the correct entry from your text file for the "to" field. i.e. You are probably sending different data to sender than you think you are (not a mobile number in this case, and thus being rejected by the API).

I would recommend running something like this, as a test, to confirm what list element you need to be sending as the "to" field.

with open('nums', 'r') as f:
    for line in f:
        line_elements = line.strip().split()
        print(line_elements)

With the code above you will see a printout of all lines in your numbers file (each line being a list of strings that are your 'columns' in your numbers file). You need to confirm which index is the number for the SMS to be sent to and then call your sender function with that index. e.g. If your text file has the "to" number as its first value in the space separated file then your code would look like this:

with open('nums', 'r') as f:
    for line in f:
        line_elements = line.strip().split()
        sender(line_elements[0])

Or, if your nums file only has the mobile numbers in it (and nothing else at all), each on a new line, then this would work:

with open('nums', 'r') as f:
    for number in f.readlines():
        sender(number.strip())

CodePudding user response:

The code may have other issues, but remove the comma after 'to':number see below

 json_data = {
        'content': smstext,
        'contentType': 'TEXT',
        'from': ' 447860002234',
        'to': number                   
    }
  • Related