1

im trying to build a simple api+lambda with chalice which can receive a POST request and then forward it to another API while adding some authentication. when i run my chalice code locally i can send a request and print the payload which i sent inside chalice console yet i cannot forward my request. i don't get back any errors or other print statements which i have in my code. i don't know where else to look for more answers so im asking here.

from chalice import Chalice
import requests

app = Chalice(app_name='redirect_url')
app.debug = True
TOKEN_URL = "https://redirect.com/v0/token"
USERNAME = 'email@email.com'
PASSWORD = 'Password01021'


@app.route('/redirect_url', methods=['POST'])
def get_payload():
    update_payload = app.current_request.json_body
    print(update_payload)


def fetch_auth_token():
    username = USERNAME
    password = PASSWORD
    data = {
        "grant_type": "password",
        "username": username,
        "password": password
    }
    response = requests.post(TOKEN_URL, data=data)
    token = response.json()
    print(token)    

IF I send a request to the endpoint (http://127.0.0.1:8000/redirect_url) I see my request printed inside chalice logs, but i don't see the token request. I would appreciate any help

wuls
  • 41
  • 5
  • I'm a bit confused by the apparent obviousness of the problem. Nowhere in your code do you call the `fetch_auth_token` function. Did you mean to put that line after the `print(update_payload)` line? – dmulter Jul 23 '18 at 19:45

1 Answers1

0

This is a bit old, but I thought I would answer it for the community for those new to Chalice.

You are expecting to fire the function without calling it. It should be:

from chalice import Chalice
import requests

app = Chalice(app_name='redirect_url')
app.debug = True
TOKEN_URL = "https://redirect.com/v0/token"
USERNAME = 'email@email.com'
PASSWORD = 'Password01021'


@app.route('/redirect_url', methods=['POST'])
def get_payload():
    update_payload = app.current_request.json_body
    APIresponse = fetch_auth_token()
    print(APIresponse) ## If you want to show it right before the update_payload
    print(update_payload)


def fetch_auth_token():
    username = USERNAME
    password = PASSWORD
    data = {
        "grant_type": "password",
        "username": username,
        "password": password
    }
    response = requests.post(TOKEN_URL, data=data)
    return response.json()
Community
  • 1
  • 1
griff4594
  • 484
  • 3
  • 15