Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么会出现“解码JSON对象失败:无法解码JSON对象”错误?_Python_Json_Flask_Flask Restplus - Fatal编程技术网

Python 为什么会出现“解码JSON对象失败:无法解码JSON对象”错误?

Python 为什么会出现“解码JSON对象失败:无法解码JSON对象”错误?,python,json,flask,flask-restplus,Python,Json,Flask,Flask Restplus,我写了以下代码: @url_api.route("/add") class AddIPvFour(Resource): """ this class contains functions to add new url. """ def post(self): """ Add a new URL map to IP or update exisitng. :return: json response object

我写了以下代码:

@url_api.route("/add")
class AddIPvFour(Resource):
    """ this class contains functions to add new url.
    """

    def post(self):
        """
            Add a new URL map to IP or update exisitng.

        :return: json response object status of newly added ip to url mapped
            or updated exisintg ip to url map.
        """
        apiAuth = None
        data = request.get_json(force=True)

        if not data:
            return jsonify({"status": "no data passed"})

        if not data["ip"]:
            return jsonify({"status" : "please pass the new ip you want to update"})
        # becuase if user has registered his API key must exist.
        if not data["apikey"]:
            return jsonify({"status": "missing API KEY"})

        apiAuth = models.APIAuth.get_apiauth_object_by_key(data["apikey"])

        if not apiAuth:
            return jsonify({"status": "invalid api key"})

        # if user exists get table name
        user_table_name = helpers.get_table_name(apiAuth.email)
        if not helpers.table_exists(user_table_name):
            user_table = helpers.create_user_table(user_table_name)
        else:
            user_table = helpers.get_user_table(user_table_name)

        # if same ip exists external port address should not exist in mapped in db.
        query_table = helpers.get_query_result(user_table_name, "ipaddress", data['ip']).fetchall()
        ports_mapped_to_ip_in_db = [item[5] for item in query_table]
        if int(data['port']) in ports_mapped_to_ip_in_db:
            return jsonify({"status": "{}:{} is already registered.".format(data["ip"], data["port"])})



        device = ""
        service = ""
        path = ""
        ipaddress = data["ip"]

        if "port" in data:
            port = data["port"]
        else:
            port = 80
        if "device" in data:
            device = data["device"]
        if "service" in data:
            service = data["service"]
        if "path" in data:
            path = data["path"]

        date_modified = datetime.now(tz=pytz.timezone('UTC'))

        urlmap = str(uuid.uuid4().get_hex().upper()[0:8])  

        field_values = {
        "ipaddress": ipaddress,
        "urlmap": urlmap,
        "port" : port,
        "device": device,
        "service": service,
        "date_created": date_modified,
        "date_modified" :date_modified,
        "path" : path,
        "count_updates": 1 if not hasattr(user_table, "count_updates") else  user_table.count_updates + 1
        }
        helpers.insert_to_table(user_table, field_values)
        result = helpers.get_query_result(user_table_name, "urlmap", urlmap)

        return jsonified(result.fetchone())
但当我试图取回:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'http://127.0.0.1:5000/api/add?API_KEY=cJRuOJyD2QdJpFpugf1QwrROKEuhSX80cRGLW6hoAC0&ip=127.0.0.1'
我试图调试,我想我从

data=request.get_jsonforce=True但我为什么要传递JSON格式! 但是,如果我使用-d标志传递数据

curl-X POST-header“内容类型:application/json”-header“接受:application/json”http://127.0.0.1:5000/api/add'-d'{API_KEY:cJRuOJyD2QdJpFpugf1QwrROKEuhSX80cRGLW6hoAC0,ip:127.0.0.1,端口:4260}'

它起作用了。它还应该与 curl-X POST-header“内容类型:application/json”-header“接受:application/json”http://127.0.0.1:5000/api/add?ip=127.0.0.2&API_KEY=cJRuOJyD2QdJpFpugf1QwrROKEuhSX80cRGLW6hoAC0'从终端或使用http://127.0.0.1:5000/api/add?ip=127.0.0.2&API_KEY=cJRuOJyD2QdJpFpugf1QwrROKEuhSX80cRGLW6hoAC0 而我的结局是

{
    "message": "Failed to decode JSON object: No JSON object could be decoded"
}
它还应该与

curl -X POST --header 'Content-Type: application/json' \
 --header 'Accept: application/json' \
'http://127.0.0.1:5000/api/add?ip=127.0.0.2API_KEY=cJRuOJyD2QdJpFpugf1QwrROKEuhSX80cRGLW6hoAC0'
从终点站

您发送了一个标题,说明您将发送一个application/json对象,但是您没有发送这样的对象,因为数据位于URL中,这本身并不常见,因为您使用的是POST方法;这样,您通常以表单编码或www多部分的形式发送信息

如果force=True,则即使您发送JSON,但不发送头,调用也可以工作。它不会以另一种方式发送头,但不会发送JSON

因此,错误消息对我来说似乎是合适的

{
    "message": "Failed to decode JSON object: No JSON object could be decoded"
}
尝试:

curl -H 'Content-Type: application/json' \
     -H 'Accept: application/json' \
     -d '{"ip":"127.0.0.2", "API_KEY":"cJRuOJyD2QdJpFpugf1QwrROKEuhSX80cRGLW6hoAC0"}' \
     'http://127.0.0.1:5000/api/add'