0

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)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Welcome to Stack Overflow. Please include the full traceback error as well as the URL that you got the error with. Please also edit your post such that the indents are correct. – ewokx Jul 04 '23 at 00:50

1 Answers1

0

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

sboyd
  • 71
  • 1
  • 3