1

Everything seems to be working fine on my end, however I am running into a single error for buy when running check50. :( buy handles fractional, negative, and non-numeric shares. expected status code 400, but got 200:

expected 400 but got 200

I believe check50 is receiving status code 200 when checking a fraction such as 1.5, before the buy form is even submitted. This is the error message I received when attempting to recreate the issue:

invalid value status 200

This error status and code are not a result of my code, and I am not exactly sure where the error is coming from or what I can do to correct this. Is it part of my buy.html form? Has anyone else see this error? I have not seen any other errors like this, if anyone can assist that would be great. Here is my buy.html form, as well as by buy function. Thanks!

"buy.html"

{% extends "layout.html" %}

{% block title %}
    Buy
{% endblock %}

{% block main %}
    <form action="/buy" method="post">
        <div class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="symbol" placeholder="Symbol" type="text"/>
        </div>
        <div class="form-group">
            <input autocomplete="off" class="form-control" min="0" name="shares" placeholder="Shares" type="number"/>
        </div>
        <button class="btn btn-primary" type="submit">Buy</button>
    </form>
{% endblock %}

buy function of application.py

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
    """Buy shares of stock"""

    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":

        # symbol of stock user would like to buy
        buy_request = lookup(request.form.get("symbol"))

        # check if symbol is valid, aplogize of not or if blank
        if buy_request == None:
        return apology("please enter valid symbol", 400)

        # keep track of cash current user has
        current_cash = db.execute("SELECT cash FROM users WHERE id=:id", id=session['user_id'])

        # confirm shares were entered
        if not request.form.get("shares"):
            return apology("must enter shares", 400)

        # number of shares user would like to purchase
        shares = float(request.form.get("shares"))

        # render an apology if the input is not a positive integer
        if shares <= 0:
            return apology("shares must be a posative integer", 400)

        # cost of once unit of stock
        price = buy_request["price"]

        # cost of total transaction
        cost = shares * price

        # amount of cash user will have left after transaction
        cash_after = current_cash[0]['cash'] - cost
        cash_after = round(cash_after, 2)

        # check if user has enough cash for transaction
        if cash_after < 0:
            return apology("You don't have enough cash...", 400)

        # log transaction into History table
        transaction = db.execute("INSERT INTO history (user_id, symbol, shares, price, cash_after) VALUES (:user_id, :symbol, :shares, :price, :cash_after)",
                             user_id=session['user_id'], symbol=buy_request["symbol"], shares=shares, price=price, cash_after=cash_after)

        # update cash for user
        update_cash = db.execute("UPDATE users SET cash = :cash_after WHERE id=:id", cash_after=cash_after, id=session['user_id'])

        # Redirect user to home page
        return redirect("/")

    # User reached route via GET (as by clicking a link or via redirect)
    else:
        return render_template("buy.html")
2

SOLVED. I added the following:

try:
    shares = int(request.form.get("shares"))
except ValueError:
    return apology("shares must be a posative integer", 400)

I was under the impression that, since I was receiving the popup error before posting /buy, there was something else causing the problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .