0

我正在尝试从 Firepower 下载对象配置,更改对象名称并将其作为新对象发布。为此,我使用 GET 方法并成功获取了对象数据。之后,我从中删除“链接”信息并尝试使用新的“名称”将其发布回来:

 import requests
 import csv
 import json
 from requests.auth import HTTPBasicAuth
 from getpass import getpass

 import urllib3
 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

 address = "###"
 username = "###"
 password = "##"

 api_uri = "/api/fmc_platform/v1/auth/generatetoken"
 url = "https://" + address + api_uri

 response = requests.request("POST", url, verify=False, auth=HTTPBasicAuth(username, password))


 accesstoken = response.headers["X-auth-access-token"]
 refreshtoken = response.headers["X-auth-refresh-token"]
 DOMAIN_UUID = response.headers["DOMAIN_UUID"]

 print("Access token: "+ accesstoken)
 print("Refresh token: "+ refreshtoken)
 print("Domain UUID: "+ DOMAIN_UUID)


def get_all_objects(address, accesstoken):
    api_uri = "/api/fmc_config/v1/domain/"+ DOMAIN_UUID +"/object/networks"
    url = "https://" + address + api_uri
    headers = {
        "X-auth-access-token": accesstoken
    }
    response = requests.get(url, verify=False, headers=headers)
    return json.loads(response.text)


def get_specific_object(address, accesstoken):
    object_id = input("Please input object ID: ")
    api_uri = "/api/fmc_config/v1/domain/"+ DOMAIN_UUID +"/object/networks/" + object_id
    url = "https://" + address + api_uri
    headers = {
        "X-auth-access-token": accesstoken
    }
    response = requests.get(url, verify=False, headers=headers)
    return json.loads(response.text)

GET_result = get_specific_object(address, accesstoken)




def add_specific_object(POST_data):
    del POST_data['links']
    POST_data["name"] = "NEW_API_ADDED"
    api_uri = "/api/fmc_config/v1/domain/"+ DOMAIN_UUID +"/object/networks/"
    url = "https://" + address + api_uri
    headers = {
        "X-auth-access-token": accesstoken
    }
    print(POST_data)

    response = requests.post(url, headers=headers, verify=False, data=json.dumps(POST_data))

    print(json.loads(response.text))
    return 


get_all_objects(address, accesstoken)
add_specific_object(GET_result)

当我在 Postman 使用相同的 JSON“POST_data”时,它会毫无问题地将该对象发布到 Firepower。但是当我从 Python 中做这件事时,它给了我:

{'error': {'category': 'FRAMEWORK', 'messages': [{'description': 'Invalid Request'}], 'severity': 
'ERROR'}}

请帮忙。看起来我的输入数据在发布之前不正确。但我不确定到底是什么。

谢谢,

4

0 回答 0