【问题标题】:Why am I getting a 403 error when making a POST request using tetpyclient?为什么使用 tetpyclient 发出 POST 请求时出现 403 错误?
【发布时间】:2020-09-08 12:30:15
【问题描述】:

尝试使用他们的 tetpyclient 在 Cisco 的 Tetration API 中发出 POST 请求。使用 get 请求时收到 200 响应,但在使用 json 过滤器发出 POST 请求以查询流时收到 403。是我的 json 数据语法不正确还是我的 python 中有语法错误?

from tetpyclient import RestClient
import urllib3
import json
import requests
from datetime import datetime
from flask import *
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

API_ENDPOINT = "https://"
restclient = RestClient(API_ENDPOINT, credentials_file = 'path/to/credentials.json', verify = False)
get = restclient.get("/flowsearch/metrics")
print (get.status_code) ##to test if api can be reached

req_payload = {"t0": "2020-08-01T09:00:00-0700",
               "t1": "2020-08-01T10:00:00-0700",
               "scopeName": "Default",
               "limit": 10,
               "filter": {                  
                  "type": "and",
                  "filters": [
                     {"type": "eq", "field": "src_address","value": "00.000.0.000" },
                     {"type": "eq", "field": "dst_address","value": "000.000.00.00" }
                   ]
}
}

resp = restclient.post('/flowsearch', json_body=json.dumps(req_payload))

print(resp.content)
print (resp.status_code)

if resp.status_code == 200:
   parsed_resp = json.loads(resp.content)
   print (json.dumps(parsed_resp, indent=4, sort_keys=True))

感谢所有反馈! 这是 tetration api 文档的链接:https://www.cisco.com/c/en/us/td/docs/security/workload_security/tetration-analytics/sw/config/b_Tetration_OpenAPI/m_flow-search.html

【问题讨论】:

    标签: python python-3.x python-2.7 api cisco


    【解决方案1】:

    HTTP 403 是一个 HTTP 状态码,表示由于某种原因禁止访问所请求的资源

    您使用的凭据似乎不允许调用/flowsearch

    【讨论】:

    • 这是我最初的想法,但我不会也为 GET 请求获得 400 或 403 吗?我对 HTTP 客户端还很陌生,所以我可能是错的,如果是权限问题,可能需要将问题升级到我上面。
    • ` GET 请求也会得到 400 或 403 ——不一定
    • get 和 post 需要不同的凭据吗?
    • @DaniMajor 没有。相同的凭据应该为您服务。
    • 好的,有什么解释为什么 get 产生 200 而 get post 产生 403?
    猜你喜欢
    • 2023-03-10
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    相关资源
    最近更新 更多