如何使用Python请求库发布帖子请求?

aba*_*sta 7 python django json cassandra python-3.x

我在Postman中使用以下过滤器在Web API中发出POST请求,但我无法在Python中使用请求库发出简单的POST请求.

首先,我发送一个POST请求到这个URL(http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets),Postman中的以下过滤器应用于Body,原始和JSON(application/json)选项被选中.

Filters in Postman

{
  "filter": {
    "filters": [
      {
        "field": "RCA_Assigned_Date",
        "operator": "gte",
        "value": "2017-05-31 00:00:00"
      },
      {
        "field": "RCA_Assigned_Date",
        "operator": "lte",
        "value": "2017-06-04 00:00:00"
      },
      {
        "field": "T_Subcategory",
        "operator": "neq",
        "value": "Temporary Degradation"
      },
      {
        "field": "Issue_Status",
        "operator": "neq",
        "value": "Queued"
      }],
     "logic": "and"
    }
}
Run Code Online (Sandbox Code Playgroud)

存储数据的数据库是Cassandra,根据以下链接,Cassandra不等于运算符,Cassandra OR运算符, Cassandra在运算符之间,Cassandra不支持NOT EQUAL TO,OR,BETWEEN运算符,所以我没办法除了AND之外,可以使用这些运算符过滤URL .

其次,我使用以下代码将一个简单的过滤器应用于请求库.

import requests
payload = {'field':'T_Subcategory','operator':'neq','value':'Temporary Degradation'}
url = requests.post("http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets",data=payload)
Run Code Online (Sandbox Code Playgroud)

但我得到的是门票的完整数据,而不仅仅是那些不是暂时退化的门票.

第三,系统实际上正在工作,但我们正在经历2-3分钟的延迟来查看数据.逻辑如下:我们有8个用户,我们希望看到每个用户的所有票证都不会暂时降级,然后我们会:

def get_json():
    if user_name == "user 001":
        with urllib.request.urlopen(
    "http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets?user_name=user&001",timeout=15) as url:
            complete_data = json.loads(url.read().decode())

    elif user_name == "user 002":
        with urllib.request.urlopen(             
    "http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets?user_name=user&002",timeout=15) as url:
            complete_data = json.loads(url.read().decode())
    return complete_data

def get_tickets_not_temp_degradation(start_date,end_date,complete_):
    return Counter([k['user_name'] for k in complete_data if start_date < dateutil.parser.parse(k.get('DateTime')) < end_date and k['T_subcategory'] != 'Temporary Degradation'])
Run Code Online (Sandbox Code Playgroud)

基本上,我们从当前和去年获得了整套票,然后我们让Python过滤用户的完整集,到目前为止只有10个用户,这意味着这个过程重复10次并且让我毫不惊讶发现为什么我们得到延迟......

我的问题是如何解决请求库的这个问题?我使用以下链接请求库文档作为教程,使其工作,但似乎我的有效负载没有被读取.

Mar*_*ers 9

你的邮递员请求是一个JSON正文.只需在Python中重现相同的主体即可.您的Python代码不发送JSON,也不发送与Postman示例相同的数据.

对于初学者来说,通过data参数发送字典会编码要application/x-www-form-urlencoded形成的字典,而不是JSON.其次,您似乎发送了一个过滤器.

以下代码完全复制了您的Postman帖子:

import requests

filters = {"filter": {
    "filters": [{
        "field": "RCA_Assigned_Date",
        "operator": "gte",
        "value": "2017-05-31 00:00:00"
    }, {
        "field": "RCA_Assigned_Date",
        "operator": "lte",
        "value": "2017-06-04 00:00:00"
    }, {
        "field": "T_Subcategory",
        "operator": "neq",
        "value": "Temporary Degradation"
    }, {
        "field": "Issue_Status",
        "operator": "neq",
        "value": "Queued"
    }],
    "logic": "and"
}}

url = "http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets"
response = requests.post(url, json=filters)
Run Code Online (Sandbox Code Playgroud)

请注意,这filters是一个Python数据结构,并将其传递给json关键字参数.使用后者有两件事:

  • 将Python数据结构编码为JSON(生成与原始Postman体值完全相同的JSON值).
  • Content-Type标题设置为application/json(正如您在Postman配置中所做的那样,在选取正文JSON后选择下拉菜单中的选项raw).

requests否则只是一个HTTP API,它不能使Cassandra做任何其他HTTP库.该urllib.request.urlopen代码发送GET请求,并平凡转换为requests具有:

def get_json():
    url = "http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets"
    response = requests.get(url, params={'user_name': user}, timeout=15)    
    return response.json()
Run Code Online (Sandbox Code Playgroud)

我删除了if分支并使用params参数替换了该参数,该参数将键值对的字典转换为正确编码的URL查询(将用户名作为user_name键传递).

注意json()对响应的调用; 这将负责解码从服务器返回的JSON数据.这仍然需要很长时间,你不是在这里过滤Cassandra数据.


tee*_*ane 5

我建议使用json属性而不是数据。它为您处理倾销。

import requests

data = {'user_name':'user&001'}
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
url = "http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets/"
r = requests.post(url, headers=headers, json=data)
Run Code Online (Sandbox Code Playgroud)

更新,问题3的答案。您使用urllib的原因吗?对于该请求,我也会使用python请求。

import requests

def get_json():
    r = requests.get("http://10.61.202.98:8081/T/a/api/rows/cat/ect/tickets”, params={"user_name": user_name.replace(" ", "&")})

    return r.json

# not sure what you’re doing here, more context/code example would help
def get_tickets_not_temp_degradation(start_date, end_date, complete_):
    return Counter([k['user_name'] for k in complete_data if start_date < dateutil.parser.parse(k.get('DateTime')) < end_date and k['T_subcategory'] != 'Temporary Degradation'])
Run Code Online (Sandbox Code Playgroud)

而且,用户名确实应该是user+001user&001还是user 001