Python 请求 : having a space in header for posting

标签 python c++ apache header python-requests

我正在尝试使用以下脚本发布到服务器:

import requests                                                                                                                                                                                                 
data = {                                                                                                      
  'query': 'GetProcess',
  'getFrom': '2018-12-06 10:10:10.000',                                                                     
}                                                                                                                                                                                               
response = requests.post('http://localhost/monitor', data=data)                                           

我找不到确切位置,但是 getFrom 元素中的空格字符被替换为 +:'2018-12-06+10: 10:10.000'

这与 SQL 在我们的服务器上期望的语法不匹配,因此查询失败。

我在这里 ( https://stackoverflow.com/a/12528097 ) 读到设置内容类型可能会有帮助。我尝试了 text/htmltext/plainapplication/json,似乎没有任何改变。

有趣的是,以下(等效的?)bash 命令成功了: curl -d 'query=GetProcess&getFrom=2018-12-06 10:10:10.000' localhost/monitor

我正在寻找一种方法让我的服务器在 header 中接收 "getFrom": "2018-12-06 10:10:10.000"

最佳答案

我找到了一种方法来完成这项工作:我遇到的问题是由于使用了 requests 中的 urlencode 函数。在 requests 文档中,展示了如何使用 PreparedRequests 绕过此默认行为:http://docs.python-requests.org/en/master/user/advanced/#prepared-requests

本质上,不是使用 requests.post() 包装器,而是显式调用函数。这样,您将能够准确控制发送的内容。就我而言,解决方案是:

import requests
data = {                                                                                                  
    'query': 'GetProcess',                                                                                                                                                                                     
    'getFrom': '2018-12-06 10:10:10.000'
}
s = requests.Session()
req = requests.Request('POST', 'http://'+ipAddress+'/monitor', data=data)
prepped = s.prepare_request(req)
prepped.body = prepped.body.replace("+", " ")
response = s.send(prepped)

关于Python 请求 : having a space in header for posting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53707202/

相关文章:

python - Python 包安装程序 exe 如何工作?

python - Django : loading fixtures with natural foreignkey fails with 'ValueError: invalid literal for int() with base 10'

C++ 从指针 vector 中删除

php - Laravel 5/Apache 2.4.9 配置问题

尝试导出数据库时 phpMyAdmin 显示空白页?

Python:使用递归算法作为生成器

python - 如何使用 Django Rest Framework 中的 SerializerMethodField 以原始形式返回列表外键字段?

c++ - 避免输出到 CppUnit 的点

c++ - C++11 中多种数值类型的 vector

php - Windows 上的 LDAP 身份验证