尝试在路径跟踪中获取 flowAnalysisId 时引发 JSON 解码错误

问题描述

我有一个 PythonScript,当我运行它时,它会引发解码错误 none。我用谷歌搜索解决方案,但无法弄清楚我的逻辑在哪里有缺陷。我理解它,因为 json 是空的并且不返回任何内容,但不知道为什么。如果相关,尝试为 cisco apic-em 做一个实验室。底部错误
我的代码

# Section 1. Setup the environment and variables required to interact with the APIC-EM
#===================================================
#+++++++++++Add Values+++++++++++++++
#import functions
import json
from requests import *
from my_apic_em_functions import *
from tabulate import *

# disable SSL certificate warnings
requests.packages.urllib3.disable_warnings()
#++++++++++++++++++++++++++++++++++++


#+++++++++++Add Values+++++++++++++++
# Path Trace API URL for flow_analysis endpoint
api_url = "https://sandBoxapicem.cisco.com/api/v1/flow-analysis"   # URL of API endpoint
# Get service ticket number using imported function
ticket = get_ticket()     # Call your function name that returns the service ticket
# Create headers for requests to the API
headers = {
        "content-type": "application/json","X-Auth-Token": ticket
        }    # Create dictionary containing headers for the request with "content-type" and "X-Auth-Token"
#++++++++++++++++++++++++++++++++++++

#============================
# Section 2. display list of devices and IPs by calling get_host() and get_devices()
#============================

#+++++++++++Add Values+++++++++++++++
# display message identifying what is to be printed on the next line
print("currently displaying hosts: ")
# Call your function name that displays hosts
print_hosts()
# display message identifying what is to be printed
print("Currently displaying network devices: ")
# Call your function name that displays network devices
print_devices()
#++++++++++++++++++++++++++++++++++++

print("\n\n")  # prints two blank lines to format output
# Section 3. Get the source and destination IP addresses for the Path Trace
# ============================

while True:
    #+++++++++++Add Values+++++++++++++++
    # Request user input for source IP address,e.g. "Please enter the source
    # IP address"
    s_ip = input("Enter source IP address: ")  # Request user input for source host IP address,e.g. "Please enter the source host IP address"
    d_ip = input("Enter destination IP address: ")  # Request user input for destination host IP address,e.g. "Please enter the destination host IP address"
    #++++++++++++++++++++++++++++++++++++
    # VarIoUs error traps Could be completed here - POSSIBLE CHALLENGE

    if s_ip != "" or d_ip != "":
        # this creates a python dictionary that will be converted to a JSON
        # object and posted
        path_data = {
            "sourceIP": s_ip,"destIP": d_ip
        }
        # Optional: Add statements that display the source and destination IP
        # addresses that will be used. And asks user to verify. Loop if not
        # verified by user.
        break  # Exit loop if values supplied
    else:
        print("\n\nYOU MUST ENTER IP ADDRESSES TO CONTINUE.\nUSE CTRL-C TO QUIT\n")
        continue  # Return to beginning of loop and repeat
# Section 4. Initiate the Path Trace and get the flowAnalysisId
#============================

#+++++++++++Add Values+++++++++++++++
# Post request to initiate Path Trace
# Convert the path_data dictionary into JSON for use in the request using json.dumps()
path = json.dumps(path_data)
# Make the request. Construct the post request to the API
resp = requests.post(api_url,path,headers=headers,verify=False)

# Inspect the return,get the Flow Analysis ID,put it into a variable
resp_json = resp.json()
flowAnalysisId = resp_json["response"][flowAnalysisId]
print("FLOW ANALYSIS ID: ",flowAnalysisId)
Traceback (most recent call last):
  File "C:\Users\Daniel\AppData\Local\Programs\Python\python39\Scripts\Section_test1.py",line 81,in <module>
    resp_json = resp.json()
  File "C:\Users\Daniel\AppData\Local\Programs\Python\python39\lib\site-packages\requests\models.py",line 900,in json
    return complexjson.loads(self.text,**kwargs)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\python39\lib\json\__init__.py",line 346,in loads
    return _default_decoder.decode(s)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\python39\lib\json\decoder.py",line 337,in decode
    obj,end = self.raw_decode(s,idx=_w(s,0).end())
  File "C:\Users\Daniel\AppData\Local\Programs\Python\python39\lib\json\decoder.py",line 355,in raw_decode
    raise JSONDecodeError("Expecting value",s,err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
>>> 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)