TypeError TypeError:'dict'对象不可调用,发布请求

时间:2019-06-13 04:58:24

标签: python post http-post

我正在尝试从python函数调用自己的Post API,在api端没有问题,它工作正常,但是当我尝试调用POST API from Python function

下面是我的代码

@app.route('/dhiru_post', methods=['GET'])
def dhiru_post():
    url = "https://www.mydomainamehere.com/api_new/"

    payload = {"number": 12524,
               "api_type": "news_listing",
               "slug": "life-mantra",
               "start": 0,
               "end": 10}
    response_decoded_json = requests.post(url, data=payload)
    response_json = response_decoded_json.json()
    return response_json
  

注意:https://www.mydomainamehere.com/api_new/并不确切   网址,请忽略。

错误: 打开这个网址: https://dhiru-ai-quantomsoftech.herokuapp.com/webhook_dhiru

TypeError
TypeError: 'dict' object is not callable

Traceback (most recent call last)
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1478, in full_dispatch_request
response = self.make_response(rv)
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1577, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/wrappers/base_response.py", line 269, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/test.py", line 1119, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'dict' object is not callable
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object

1 个答案:

答案 0 :(得分:1)

您可以按以下方式修改处理程序:

from flask import jsonify

@app.route('/dhiru_post', methods=['GET'])
def dhiru_post():
    url = "https://www.mydomainamehere.com/api_new/"
    payload = {
        "number": 12524,
        "api_type": "news_listing",
        "slug": "life-mantra",
        "start": 0,
        "end": 10
    }
    response = requests.post(url, data=payload)
    return jsonify(response.json())

因此,基本上,您需要返回一个响应而不是字典,jsonify将为您做到这一点:设置正确的内容类型并将response.json()字典转换为字符串并将其用作内容。