Error in Python Flask execution in PythonAnywhere from TwelveData

47 Views Asked by At

I have to create working link via pythonanywhere. Below is my code to accept stock symbol and respond with the name and price. The below is my code. However, I'm getting the following error:

Method Not Allowed
The method is not allowed for the requested URL.

Is the error with the code or the website?

import flask
from flask import Flask, jsonify, request
import requests

from geopy.geocoders import Nominatim

def get_stock_price(ticker_symbol, api):
    url = f"https://api.twelvedata.com/price?symbol={ticker_symbol}&apikey={api}"
    response = requests.get(url).json()
    price = response['price'][:-3]
    return price

def get_stock_quote(ticker_symbol, api):
    url = f"https://api.twelvedata.com/quote?symbol={ticker_symbol}&apikey={api}"
    response = requests.get(url).json()
    return response

api_key = "<api_key>"

app = Flask(__name__)

@app.route('/', methods=['POST'])
def webhook():
    body = request.json
    ticker = body.get('queryResult', {}).get('parameters', {}).get('Share')
    if ticker:
    stock_details = get_stock_quote(ticker, api_key)
    stock_price = get_stock_price(ticker, api_key)
    name = stock_details['name']
    response = {'fulfillmentText': f'The price of {name} is {stock_price}'}
    else:
    response = {
    'fulfillmentText': 'No ticker symbol provided.'
    }
    return jsonify(response)

if __name__ == '__main__':
    app.run(debug=True)
1

There are 1 best solutions below

0
sboyd On

The error suggests that the request to "/" is being made as a GET request. On the line @app.route('/', methods=['POST']) you can see that you're only allowing POST requests