PinkeshGjr
PinkeshGjr

Reputation: 8680

Python Flask Request is not working with multiple parameter

i was try to use python API but its not working if i try to use multiple parameter

Not working

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)

Working

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)

Error

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

Logs

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)

Upvotes: 0

Views: 500

Answers (3)

Adam
Adam

Reputation: 4172

I honestly can't tell where the issue w/ your indents is and whether that is a misunderstanding how whitespacing works in python or posting code blocks on stackoverflow (my guess is a combo of both). So I took your code and put it in PyCharm and properly indented it and pasted that code into this nice tool I just found so I could properly submit it. This should hopefully resolve your issues. Just copy and paste it then change all the necessary values.

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) 

Upvotes: 2

Jim Wright
Jim Wright

Reputation: 6058

The hint is in your logs.

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

You have bad indentation in server.py on line 29.

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)

should look like:

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)

It looks like you have loads of other badly indented lines as well.

Upvotes: 0

sivan
sivan

Reputation: 111

as you can see in the logs ,the app crashed due to indentation error. please check indentation of account_sid variable in your code.

Upvotes: 1

Related Questions