【问题标题】:TypeError TypeError: 'dict' object is not callable , post requestTypeError TypeError: 'dict' object is not callable , post request
【发布时间】:2019-06-13 04:58:24
【问题描述】:

我正在尝试从 python 函数调用我自己的Post API,在 api 方面没有问题,它工作正常,但是当我尝试调用 POSTAPI 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

【问题讨论】:

  • 问题是return response_json。请求成功,请确保支持response_json的类型。
  • @Sraw 我是 python 的初学者,我不知道我将如何解决这个问题,您能否发布一些示例以用于发布请求,其中包含 JSON 输入和 JSON 输出

标签: python post http-post


【解决方案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() 字典转换为字符串并将其用作内容。

【讨论】:

  • 需要导入from flask import jsonify
  • 好的,另外,在 post 请求中未发送到服务器的参数,我是否也需要使用 jsonify 进行转换??
  • 嗨,你的回答是有效的,我会接受,你能帮我把参数发送到服务器吗,现在也不例外,但我仍然无法在你的例子中发送参数
  • 您好,请注意您正在发送 POST 请求,因此数据包含在请求正文中,而不是作为查询参数。有关更多信息,请在此处阅读此答案stackoverflow.com/a/16664376/4586108
  • 在 Starlette 中你可以使用return JSONResponse(response)
猜你喜欢
  • 2014-10-19
  • 1970-01-01
  • 2018-03-31
  • 2021-10-22
  • 1970-01-01
  • 2019-04-07
  • 1970-01-01
  • 2022-08-23
  • 2022-12-26
相关资源
最近更新 更多