OverworkedTechydude
OverworkedTechydude

Reputation: 271

Function works with POST but fails with GET request

I'm building an api that interfaces with various sms providers and allows users to send and receive texts. I'm using Flask and Python 3.4. My issue is that when I try to verify an account it works fine if I send data using a POST request. But if I use a GET request I get an error telling me to authenticate. Here is my function:

@coma_inbound.route("/twilio/verify/account",methods=["GET","POST"])
def verifyAccount():
    #pdb.set_trace()
    account_sid = request.values.get("account")
    auth_token = request.values.get("credentials")
    targetAcct = request.values.get("targetAcct")
    print(account_sid, auth_token, targetAcct)
    try:
        client = TwilioRestClient(account_sid, auth_token)
        print(client.auth)
        print("authenticated")
    except TwilioRestException as e:
        print(e)
        print("Updating Status 1")
        status = str(e.msg)[:250]
        print(status)
        return status
    try:
        print(account_sid, auth_token, targetAcct)
        print(client.auth)
        account = client.accounts.get(targetAcct)
        status = account.status
    except TwilioRestException as e:
        print(e)
        print("Updating Status 2")
        status = str(e.msg)[:250]
        print(status)
        return status   
    print(status)
    return status

My POST request is this:

curl -vvv --data "account=ACf7e45c1e1547c066005efe64f933aa45&credentials=6d76c0bab837a10e6763a61aabacf7f2&targetAcct=ACf7e45c1e1547c066005efe64f933aa45" http://127.0.0.1:5000/twilio/verify/account

Which outputs this from curl (active is expected result):

* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /twilio/verify/account HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:5000
> Accept: */*
> Content-Length: 133
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 133 out of 133 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 6
< Server: Werkzeug/0.11.3 Python/3.4.3
< Date: Fri, 04 Mar 2016 14:43:28 GMT
< 
* Closing connection 0
active

My GET request is this:

curl -vvv http://127.0.0.1:5000/twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45&credentials=6d76c0bab837a10e6763a61aabacf7f2&targetAcct=ACf7e45c1e1547c066005efe64f933aa45

Which outputs this to curl:

[1] 6875
[2] 6876
anon@anon-VirtualBox:~/Coma$ * Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> GET /twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:5000
> Accept: */*
> 
* HTTP 1.0, assume close after body
< HTTP/1.0 500 INTERNAL SERVER ERROR
< Content-Type: text/html
< Content-Length: 291
< Server: Werkzeug/0.11.3 Python/3.4.3
< Date: Fri, 04 Mar 2016 15:01:51 GMT
< 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>
* Closing connection 0

At this point it hangs until I ctrl+c then outputs this:

^C
[1]-  Done                    curl -vvv http://127.0.0.1:5000/twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45
[2]+  Done                    credentials=6d76c0bab837a10e6763a61aabacf7f2

My error is:

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 159-528-786
Received: ACf7e45c1e1547c066005efe64f933aa45 None None
127.0.0.1 - - [04/Mar/2016 09:53:28] "GET /twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45 HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/anon/Coma/Inbound/FlaskComa/views.py", line 68, in verifyAccount
    client = TwilioRestClient(account_sid, auth_token)
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/client.py", line 49, in __init__
    timeout)
  File "/usr/local/lib/python3.4/dist-packages/twilio/rest/base.py", line 57, in __init__
    """)
twilio.exceptions.TwilioException: 
Twilio could not find your account credentials. Pass them into the
TwilioRestClient constructor like this:

    client = TwilioRestClient(account='AC38135355602040856210245275870',
                              token='2flnf5tdp7so0lmfdu3d')

Or, add your credentials to your shell environment. From the terminal, run

    echo "export TWILIO_ACCOUNT_SID=AC3813535560204085626521" >> ~/.bashrc
    echo "export TWILIO_AUTH_TOKEN=2flnf5tdp7so0lmfdu3d7wod" >> ~/.bashrc

and be sure to replace the values for the Account SID and auth token with the
values from your Twilio Account at https://www.twilio.com/user/account.

Upvotes: 3

Views: 299

Answers (1)

Eugene Lisitsky
Eugene Lisitsky

Reputation: 12845

Please have a look at the curl parameters. In second case you run it without double quotes - so shell parses the string. But "&" have a special meaning - run program in background. So in this case you run one script

curl -vvv http://127.0.0.1:5000/twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45

in background mode and another one

credentials=6d76c0bab837a10e6763a61aabacf7f2

in foreground.

As a result your python script does not get credentials and fails:

GET /twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45 HTTP/1.1

Quotes single or double will help.

Yours, Eugene

Upvotes: 3

Related Questions