Python Flask请求无法使用多个参数
我曾尝试使用pythonapi,但如果我尝试使用多参数,它将不起作用 不工作Python Flask请求无法使用多个参数,python,heroku,flask,request,twilio-api,Python,Heroku,Flask,Request,Twilio Api,我曾尝试使用pythonapi,但如果我尝试使用多参数,它将不起作用 不工作 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 =
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)
错误
日志
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)
正如您在日志中看到的,应用程序由于缩进错误而崩溃。
请检查代码中帐户sid变量的缩进。正如您在日志中看到的,由于缩进错误,应用程序崩溃。
请检查代码中帐户sid变量的缩进。提示在您的日志中
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)
看起来您还有大量其他缩进严重的行。提示在您的日志中
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)
看起来还有很多其他缩进严重的行。老实说,我不知道缩进的问题在哪里,也不知道这是对python中空格工作原理的误解,还是在stackoverflow上发布代码块的误解(我猜是两者的结合)。所以我把你的代码放在PyCharm中,正确地缩进,然后把代码粘贴到我刚刚找到的这个漂亮的文件中,这样我就可以正确地提交它了。这将有望解决您的问题。只需复制并粘贴它,然后更改所有必要的值
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)
老实说,我不知道缩进的问题在哪里,也不知道这是否是对python中空格工作原理的误解,还是在stackoverflow上发布代码块的误解(我猜是两者的结合)。所以我把你的代码放在PyCharm中,正确地缩进,然后把代码粘贴到我刚刚找到的这个漂亮的文件中,这样我就可以正确地提交它了。这将有望解决您的问题。只需复制并粘贴它,然后更改所有必要的值
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)
请用正确的错误描述和完整的错误回溯来完成您的问题。您应该阅读并遵循给定的建议。添加的详细日志也请帮助我们解决此问题谢谢您的代码在第29行的缩进格式不正确。如何解决此错误?请用正确的错误描述和完整的错误回溯来完成您的问题错误回溯。您应该阅读它并遵循给定的建议。添加的详细信息日志也请帮助我们解决此问题谢谢您的代码在第29行的缩进格式不正确。如何解决此错误?添加了更多详细信息请查看添加了更多详细信息请查看添加了更多详细信息请查看添加了更多详细信息请查看缩进错误在原始问题中提交的格式化代码…我本来打算提交对问题的编辑,以便我可以实际阅读代码,但当我看到错误是什么时,我意识到这是不可能的。添加了更多详细信息请查看原始问题中提交的格式不正确的代码的缩进错误…我打算向这样我就可以真正地阅读代码了,但当我看到错误是什么时,我意识到这是不可能的。