python - 我如何发送带有 python 和动态值的发布请求?

标签 python http post python-requests

过去几个小时我一直在四处寻找,也许是我在 python 方面缺乏经验。我在网上找到这段代码:

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

我想做的是发送一个 HTTP POST 请求,请求看起来像这样:

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

我有以下参数:to,from,message,email,api_secret

email 和 api_secret 应该硬编码到代码本身。 所以我想做的是从用户那里收集以下信息:

收件人、发件人、消息

接受它并将其作为 POST 请求发送到上面的 url。

问题是,我不知道如何将其实现到我的代码中。非常感谢您的帮助。 谢谢!

最佳答案

正如您所提到的:

# send the POST request

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

因此,只需创建一个 params 变量,如下所示:

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

并使用 requests.post(url,params=params) 发布它。

如果您还有一些数据要发布,那么您可以类似地创建一个字典,并添加 data 参数。

关于python - 我如何发送带有 python 和动态值的发布请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64110020/

相关文章:

ruby-on-rails - Rails send_data 返回 HTTP 错误 406

javascript - Angular 服务调用 API 导致 switchmap 错误

javascript - NoReverseMatch at/blog/in django 2.0

python - Notepad++ 的 Python 脚本中的可选参数

python - 出现错误 :name 'webdriver' is not defined for appium

http持久连接和ssl session

laravel - Vuejs Axios POST 请求从 Laravel 后端获取 HTTP422 错误

http - Angular 2 POST 请求

node.js - aws lambda 调用未在 POST 上填充正文

python - 在 for 循环字符串中切换单词