【问题标题】:Python Flask Request is not working with multiple parameterPython Flask Request 不适用于多个参数
【发布时间】:2017-07-11 06:03:59
【问题描述】:

我曾尝试使用 python API,但如果我尝试使用多个参数,它就无法正常工作

不工作

from flask import Flask, request

    @app.route('/test', methods=['GET', 'POST'])
    def test():

        req_json = request.get_json(force=True)
        UserName = req_json['username']
        UserPassword = req_json['password']
        return str(UserName)

工作

from flask import Flask, request

    @app.route('/test', methods=['GET', 'POST'])
    def test():

        req_json = request.get_json(force=True)
        UserName = req_json['username']
        return str(UserName)

错误

https://www.herokucdn.com/error-pages/application-error.html

日志

State changed from crashed to starting
2017-07-11T06:44:13.760404+00:00 heroku[web.1]: Starting process with command `python server.py`
2017-07-11T06:44:16.078195+00:00 app[web.1]:   File "server.py", line 29
2017-07-11T06:44:16.078211+00:00 app[web.1]:     account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
2017-07-11T06:44:16.078211+00:00 app[web.1]:     ^
2017-07-11T06:44:16.078213+00:00 app[web.1]: IndentationError: unexpected indent
2017-07-11T06:44:16.179785+00:00 heroku[web.1]: Process exited with status 1
2017-07-11T06:44:16.192829+00:00 heroku[web.1]: State changed from starting to crashed

Server.py

    import os
    from flask import Flask, request
    from twilio.jwt.access_token import AccessToken, VoiceGrant
    from twilio.rest import Client
    import twilio.twiml

    ACCOUNT_SID = 'accountsid'
    API_KEY = 'apikey'
    API_KEY_SECRET = 'apikeysecret'
    PUSH_CREDENTIAL_SID = 'pushsid'
    APP_SID = 'appsid'


    app = Flask(__name__)

    @app.route('/test', methods=['GET', 'POST'])
    def test():

        req_json = request.get_json(force=True)
        UserName = req_json['username']
        Password = req_json['password']
        return str(UserName)

    @app.route('/accessToken')
    def token():

req_json = request.get_json(force=True)
        IDENTITY = req_json['identity']

            account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
                api_key = os.environ.get("API_KEY", API_KEY)
                    api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
                        push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
                            app_sid = os.environ.get("APP_SID", APP_SID)

                                grant = VoiceGrant(
                                                   push_credential_sid=push_credential_sid,
                                                   outgoing_application_sid=app_sid
                                                   )

                                    token = AccessToken(account_sid, api_key, api_key_secret, IDENTITY)
                                        token.add_grant(grant)

                                            return str(token)

    @app.route('/outgoing', methods=['GET', 'POST'])
    def outgoing():
        resp = twilio.twiml.Response()
            #resp.say("Congratulations! You have made your first oubound call! Good bye.")
            resp.say("Thanks for Calling! Please try again later.")
                return str(resp)

    @app.route('/incoming', methods=['GET', 'POST'])
    def incoming():
        resp = twilio.twiml.Response()
            #resp.say("Congratulations! You have received your first inbound call! Good bye.")
            resp.say("Thanks for Calling! Please try again later.")
                return str(resp)

    @app.route('/placeCall', methods=['GET', 'POST'])
    def placeCall():

        req_json = request.get_json(force=True)
            IDENTITY = req_json['identity']
                CALLER_ID = req_json['callerid']

                    account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
                        api_key = os.environ.get("API_KEY", API_KEY)
                            api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)

                                client = Client(api_key, api_key_secret, account_sid)
                                    call = client.calls.create(url=request.url_root + 'incoming', to='client:' + CALLER_ID, from_='client:' + IDENTITY)
                                        return str(call.sid)

    @app.route('/', methods=['GET', 'POST'])
    def welcome():
        resp = twilio.twiml.Response()
            resp.say("Welcome")
                return str(resp)

    if __name__ == "__main__":
        port = int(os.environ.get("PORT", 5000))
            app.run(host='0.0.0.0', port=port, debug=True)

【问题讨论】:

  • 请用正确的错误描述和完整的错误回溯完成您的问题。
  • 你应该阅读它并遵循给定的建议。
  • 添加详细日志也请帮助我们解决这个问题,谢谢
  • 您的代码的缩进格式不正确@line 29。
  • 我该如何解决这个错误?

标签: python heroku flask request twilio-api


【解决方案1】:

老实说,我不知道你的缩进问题出在哪里,以及这是否是对 python 中的空白如何工作或在 stackoverflow 上发布代码块的误解(我的猜测是两者的结合)。因此,我将您的代码放入 PyCharm 中并正确缩进并将该代码粘贴到我刚刚找到的这个漂亮的tool 中,以便我可以正确提交它。这应该有望解决您的问题。只需复制并粘贴它,然后更改所有必要的值。

import os
from flask import Flask, request
from twilio.jwt.access_token import AccessToken, VoiceGrant
from twilio.rest import Client
import twilio.twiml

ACCOUNT_SID = 'accountsid'
API_KEY = 'apikey'
API_KEY_SECRET = 'apikeysecret'
PUSH_CREDENTIAL_SID = 'pushsid'
APP_SID = 'appsid'


app = Flask(__name__)

@app.route('/test', methods=['GET', 'POST'])
def test():
    req_json = request.get_json(force=True)
    UserName = req_json['username']
    Password = req_json['password']
    return str(UserName)

@app.route('/accessToken')
def token():
    req_json = request.get_json(force=True)
    IDENTITY = req_json['identity']

    account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
    api_key = os.environ.get("API_KEY", API_KEY)
    api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
    push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
    app_sid = os.environ.get("APP_SID", APP_SID)

    grant = VoiceGrant(
        push_credential_sid=push_credential_sid,
        outgoing_application_sid=app_sid
    )

    token = AccessToken(account_sid, api_key, api_key_secret, IDENTITY)
    token.add_grant(grant)

    return str(token)

@app.route('/outgoing', methods=['GET', 'POST'])
def outgoing():
    resp = twilio.twiml.Response()
    #resp.say("Congratulations! You have made your first oubound call! Good bye.")
    resp.say("Thanks for Calling! Please try again later.")
    return str(resp)

@app.route('/incoming', methods=['GET', 'POST'])
def incoming():
    resp = twilio.twiml.Response()
    #resp.say("Congratulations! You have received your first inbound call! Good bye.")
    resp.say("Thanks for Calling! Please try again later.")
    return str(resp)

@app.route('/placeCall', methods=['GET', 'POST'])
def placeCall():

    req_json = request.get_json(force=True)
    IDENTITY = req_json['identity']
    CALLER_ID = req_json['callerid']

    account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
    api_key = os.environ.get("API_KEY", API_KEY)
    api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)

    client = Client(api_key, api_key_secret, account_sid)
    call = client.calls.create(url=request.url_root + 'incoming', to='client:' + CALLER_ID, from_='client:' + IDENTITY)
    return str(call.sid)

@app.route('/', methods=['GET', 'POST'])
def welcome():
    resp = twilio.twiml.Response()
    resp.say("Welcome")
    return str(resp)

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port, debug=True) 

【讨论】:

    【解决方案2】:

    正如您在日志中看到的那样,应用程序由于缩进错误而崩溃。 请检查代码中 account_sid 变量的缩进。

    【讨论】:

    • 添加更多细节请看
    【解决方案3】:

    提示在您的日志中。

    2017-07-11T06:44:16.078195+00:00 app[web.1]:   File "server.py", line 29
    2017-07-11T06:44:16.078211+00:00 app[web.1]:     account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
    2017-07-11T06:44:16.078211+00:00 app[web.1]:     ^
    2017-07-11T06:44:16.078213+00:00 app[web.1]: IndentationError: unexpected indent
    

    您在第 29 行的 server.py 中有错误的缩进。

    req_json = request.get_json(force=True)
            IDENTITY = req_json['identity']
    
                account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
                    api_key = os.environ.get("API_KEY", API_KEY)
                        api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
                            push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
                                app_sid = os.environ.get("APP_SID", APP_SID)
    

    应该看起来像:

    req_json = request.get_json(force=True)
    IDENTITY = req_json['identity']
    account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
    api_key = os.environ.get("API_KEY", API_KEY)
    api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
    push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
    app_sid = os.environ.get("APP_SID", APP_SID)
    

    看起来你还有很多其他严重缩进的行。

    【讨论】:

    • 添加更多细节请查看
    • 在原始问题中提交格式错误的代码的缩进错误...我将提交对该问题的编辑,以便我可以实际阅读代码但是当我看到错误是什么时,我意识到那是'不可能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 2020-03-16
    • 2014-08-07
    相关资源
    最近更新 更多