How can i send a post request with python & dynamic values?

Viewed 1404

I have spent the past couple of hours looking arround, perhaps its the lack of experience i have in python. I found this code online:

import requests # you have to install this library, with pip for example

# define your custom headers (as many as you want)

headers = {
  'X-Prototype-Version': '1.7.2'
}

# define your URL params (!= of the body of the POST request)

params = {
  'your_first_param': 'its_value',
  'your_second_param': 'its_value'
}

# define the body of the POST request

data = {
  'message' : 'your message'
}

# send the POST request

response = requests.post('https://example.com/index.php', params=params, data=data, headers=headers)

# here is the response

print response.text

What i'm trying to do is send a HTTP POST request and the request looks like this:

https://example.com/dashboard/api?to={PHONE NUMBER}&from={SENDER ID}&message={TEXT}&email={YOUREMAIL}&api_secret={API SECRET}

I have the following paremters: to,from,message,email,api_secret.

email and api_secret should be hardcoded into the code itself. So what im trying to do is collect the following info from the user:

To,From,Message

Take that and send it off as a POST request to the url up there.

The thing is, i have no idea how to implement that into my code. Help is highly appreciated. Thank you!

1 Answers

As you've mentioned:

# send the POST request

response = requests.post('https://example.com/index.php', params=params, data=data, headers=headers)

So, just create a params variable, like this:

params = {
    'from':from,
    'to':to,
    'message':message,
    'email':hardCodedEmail,
    'api_secret':hardCodedSecret,
}

And post it using requests.post(url,params=params).

If you also have some data to post, then you can similarly create a dict, and add the data parameter.

Related