简体   繁体   English

在 python 中向 json 正文请求添加值

[英]add a value to a json body request in python

I have created my own function which i import called timestamp, it returns two values:我创建了自己的 function,我将其导入称为时间戳,它返回两个值:

from requests.auth import HTTPBasicAuth
import requests
import json


def timeframe():
    response = requests.get("https://$host/api/profiler/1.13/reporting/timestamps.json", verify=False, auth=HTTPBasicAuth("admin", "admin"))
    time = response.json()
    for entry in time:
        if entry.get('data_resolution') == 'min':
            if entry.get('datasource') == 'FDS_TRAFFIC':
                start_time = entry['start_time']
                end_time = entry['end_time']
                return start_time, end_time

timeframe()

i need to add timestamps to a keys in a json body request, you will see 'end' & 'start' keys.我需要为 json 正文请求中的键添加时间戳,您将看到“结束”和“开始”键。 I need to retrieve those timestamps and somehow add them to those keys.我需要检索这些时间戳并以某种方式将它们添加到这些键中。

    import requests
import timestamp
from requests.auth import HTTPBasicAuth
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

stamp = timestamp.timeframe()

print(stamp)

url = 'http://10.65.170.112/api/profiler/1.12/reporting/reports'
headers = {'Content-Type': 'application/json'}
payload = {
    "criteria": {
        "time_frame": {
            "start": str(stamp[0]),
            "end": str(stamp[1]),
            "resolution": "flow"
        },
        "query": {
            "realm": "traffic_flow_list",
            "sort_column": 41,
            "devices": [
        {
          "ipaddr": "10.65.170.2"
        }
      ],
            "group_by": "flw",
            "columns": [
                729,
                40,
                41,
                14,
                44,
                10,
                45,
                46
            ]
        }
    },
    "template_id": 184
}

req = requests.post(url,  headers=headers, data = payload, verify=False, auth=HTTPBasicAuth('admin', 'admin'),)

print(req.status_code, req.text)

Not sure what to do.不知道该怎么办。

Thanks谢谢

Tudo bem?图多贝姆?

The function you created returns a tuple: return start_time, end_time .您创建的 function 返回一个元组: return start_time, end_time

So, a way to implement would be:因此,一种实现方式是:

start, end = timestamp.timeframe()

Then, you can hydrate your body:然后,您可以为身体补充水分:

body = {  
    "criteria": {  
        "time_frame": {  
            "end": end,  
            "start": start,  
            "resolution": "flow" 
},

I hope that this helps.我希望这个对你有用。

import requests
import timestamp
from requests.auth import HTTPBasicAuth
import urllib3
import json
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

stamp = timestamp.timeframe()
auth = HTTPBasicAuth("admin", "admin")

url = "https://$host/api/profiler/1.13/reporting/reports"

headers = {'Content-Type': 'application/json'}

payload = {
    "criteria": {
        "time_frame": {
            "start": stamp[0],
            "end": stamp[1],
            "resolution": "flow"
        },
        "query": {
            "realm": "traffic_summary",
            "sort_column": 41,
            "devices": [{ "ipaddr": "10.65.170.2"}],
            "group_by": "hos",
            "columns": [
                729,
                40,
                41,
                14,
                44,
                10,
                45,
                46
            ]
        }
    },
    "template_id": 184
}

req = requests.post(url, verify=False, auth=auth, headers=headers, data=json.dumps(payload))

print(req.headers)

Was resolved by adding data=json.dump(payload)通过添加 data=json.dump(payload) 解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM