python - Why is this throwing requests.exceptions.HTTPError? - Stack Overflow

admin2025-04-27  3

I'm trying to get some data from a form on my website using Flask and it's throwing a requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: and I don't really know how to solve this I've been trying for like an hour (i'm very much a beginner, maybe intermediate in python)


I've tried to use try, except blocks, I changed the date input to a string and I tried some other things like testing another embed with the same webhook and it worked so now I really am not sure what the problem is, now what was supposed to happen is, i enter the details into the form and it goes through the discord webhook and send the details in an embed.


Here's my HTML:

<div class="logger">
        <h1>Log a job:</h1>
        <form method="post">
          <div class="left-form">
            <input type="text" name="id" placeholder="ID" required />
            <input
              type="text"
              name="date"
              id="date"
              placeholder="Date (DD.MM.YYYY)"
            />
            <input
              type="text"
              name="from"
              placeholder="From (Company, City, Country)"
              required
            />
            <input
              type="text"
              name="to"
              placeholder="To (Company, City, Country)"
              required
            />
            <input type="text" name="cargo" placeholder="Cargo" required />
            <input type="number" name="weight" placeholder="Weight" required />
          </div>
          <div class="right-form">
            <input
              type="number"
              name="distance"
              placeholder="Distance"
              required
            />
            <input type="number" name="price" placeholder="Price" required />
            <input type="number" name="fuel" placeholder="Fuel (L)" required />
            <input
              type="text"
              name="truck"
              placeholder="Truck (Brand, Model)"
              required
            />
            <input
              type="text"
              name="trailer"
              placeholder="Trailer (Brand, Model)"
              required
            />
            <input type="checkbox" name="isEts" value="Yes" />
            <label for="isEts">ETS2</label>
            <input type="checkbox" name="isAts" value="Yes" />
            <label for="isAts">ATS</label>
          </div>
          <div class="log-btn">
            <button type="submit">Log</button>
          </div>
        </form>
      </div>

---

and here's my python:

# git add .
#git commit -m "commit"
#git push origin master


from config import webhook, log_hook #vars rfrom config.py
#from config import th_api
from flask import Flask, render_template, jsonify, request
import requests
from flask_cors import CORS
from dhooks import Webhook, Embed

contactHook = Webhook(webhook)
logHook = Webhook(log_hook)
app = Flask(__name__)
CORS(app)


@app.route('/')
def home():
    return render_template('index.html')

@app.route('/inter')
def inter():
    return render_template('inter.html')

@app.route('/reefer')
def reefer():
    return render_template('reefer.html')

@app.route("/blog")
def blog():
    return render_template("blog.html")



@app.route('/contact', methods=['POST', 'GET'])
def contact():
    if request.method == 'POST':
        name = request.form.get('name')
        email = request.form.get('email')
        discord = request.form.get('discord')
        subject = request.form.get('subject')
        message = request.form.get('msgbody')

        # Create the embed inside the route
        contactembed = Embed(
            description="",
            color=0x000000,
            timestamp='now'
        )
        img1 = ".png"
        hex = ".jpeg"
        contactembed.set_author(name='Hexx', icon_url=hex)
        contactembed.add_field(name='Name: ', value=name)
        contactembed.add_field(name='Email: ', value=email)
        contactembed.add_field(name='Discord: ', value=discord)
        contactembed.add_field(name='Subject: ', value=subject)
        contactembed.add_field(name='Message: ', value=message)
        contactembed.set_thumbnail(img1)
        
        # Send the webhook
        contactHook.send(embed=contactembed)

    return render_template('contact.html')

@app.route('/apply')
def apply():
    return render_template('apply.html')

@app.route('/workspace/manual', methods=['POST', 'GET'])
def workspace():
    if request.method == 'POST':
        id = request.form.get('id')
        date = request.form.get('date')
        start = request.form.get('start')
        end = request.form.get('end')
        cargo = request.form.get('cargo')
        weight = request.form.get('weight')
        dist = request.form.get('distance')
        price = request.form.get('price')
        fuel = request.form.get('fuel')
        truck = request.form.get('truck')
        trailer = request.form.get('trailer')
        isAts = request.form.get('isAts')
        isEts2 = request.form.get('isEts2')
        
        logembed = Embed(
            description="",
            color=0x000000,
            timestamp='now'
        )
        img1 = ".png"
        hex = ".jpeg"
        logembed.set_author(name='Hexx', icon_url=hex)
        logembed.set_thumbnail(img1)
        logembed.add_field(name='ID: ', value=id)
        logembed.add_field(name='Date: ', value=date)
        logembed.add_field(name='From: ', value=start)
        logembed.add_field(name='To: ', value=end)
        logembed.add_field(name='Cargo: ', value=cargo)
        logembed.add_field(name='Weight: ', value=f"{weight}T")
        logembed.add_field(name='Distance: ', value=f"{dist}KM")
        logembed.add_field(name='Price: ', value=f"{price}€")
        logembed.add_field(name='Fuel: ', value=f"{fuel}L")
        logembed.add_field(name='Truck: ', value=truck)
        logembed.add_field(name='Trailer: ', value=trailer)
        if isAts == 'Yes':
            logembed.add_field(name='Game: ', value='ATS')
        elif isEts2 == 'Yes':
            logembed.add_field(name='Game: ', value='ETS2')
        
        logHook.send(embed=logembed)
        
        
    return render_template('work_manual.html')
    

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

I'm trying to get some data from a form on my website using Flask and it's throwing a requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: and I don't really know how to solve this I've been trying for like an hour (i'm very much a beginner, maybe intermediate in python)


I've tried to use try, except blocks, I changed the date input to a string and I tried some other things like testing another embed with the same webhook and it worked so now I really am not sure what the problem is, now what was supposed to happen is, i enter the details into the form and it goes through the discord webhook and send the details in an embed.


Here's my HTML:

<div class="logger">
        <h1>Log a job:</h1>
        <form method="post">
          <div class="left-form">
            <input type="text" name="id" placeholder="ID" required />
            <input
              type="text"
              name="date"
              id="date"
              placeholder="Date (DD.MM.YYYY)"
            />
            <input
              type="text"
              name="from"
              placeholder="From (Company, City, Country)"
              required
            />
            <input
              type="text"
              name="to"
              placeholder="To (Company, City, Country)"
              required
            />
            <input type="text" name="cargo" placeholder="Cargo" required />
            <input type="number" name="weight" placeholder="Weight" required />
          </div>
          <div class="right-form">
            <input
              type="number"
              name="distance"
              placeholder="Distance"
              required
            />
            <input type="number" name="price" placeholder="Price" required />
            <input type="number" name="fuel" placeholder="Fuel (L)" required />
            <input
              type="text"
              name="truck"
              placeholder="Truck (Brand, Model)"
              required
            />
            <input
              type="text"
              name="trailer"
              placeholder="Trailer (Brand, Model)"
              required
            />
            <input type="checkbox" name="isEts" value="Yes" />
            <label for="isEts">ETS2</label>
            <input type="checkbox" name="isAts" value="Yes" />
            <label for="isAts">ATS</label>
          </div>
          <div class="log-btn">
            <button type="submit">Log</button>
          </div>
        </form>
      </div>

---

and here's my python:

# git add .
#git commit -m "commit"
#git push origin master


from config import webhook, log_hook #vars rfrom config.py
#from config import th_api
from flask import Flask, render_template, jsonify, request
import requests
from flask_cors import CORS
from dhooks import Webhook, Embed

contactHook = Webhook(webhook)
logHook = Webhook(log_hook)
app = Flask(__name__)
CORS(app)


@app.route('/')
def home():
    return render_template('index.html')

@app.route('/inter')
def inter():
    return render_template('inter.html')

@app.route('/reefer')
def reefer():
    return render_template('reefer.html')

@app.route("/blog")
def blog():
    return render_template("blog.html")



@app.route('/contact', methods=['POST', 'GET'])
def contact():
    if request.method == 'POST':
        name = request.form.get('name')
        email = request.form.get('email')
        discord = request.form.get('discord')
        subject = request.form.get('subject')
        message = request.form.get('msgbody')

        # Create the embed inside the route
        contactembed = Embed(
            description="",
            color=0x000000,
            timestamp='now'
        )
        img1 = "https://i.imgur.com/5s6i94n.png"
        hex = "https://i.imgur.com/6JxudkT.jpeg"
        contactembed.set_author(name='Hexx', icon_url=hex)
        contactembed.add_field(name='Name: ', value=name)
        contactembed.add_field(name='Email: ', value=email)
        contactembed.add_field(name='Discord: ', value=discord)
        contactembed.add_field(name='Subject: ', value=subject)
        contactembed.add_field(name='Message: ', value=message)
        contactembed.set_thumbnail(img1)
        
        # Send the webhook
        contactHook.send(embed=contactembed)

    return render_template('contact.html')

@app.route('/apply')
def apply():
    return render_template('apply.html')

@app.route('/workspace/manual', methods=['POST', 'GET'])
def workspace():
    if request.method == 'POST':
        id = request.form.get('id')
        date = request.form.get('date')
        start = request.form.get('start')
        end = request.form.get('end')
        cargo = request.form.get('cargo')
        weight = request.form.get('weight')
        dist = request.form.get('distance')
        price = request.form.get('price')
        fuel = request.form.get('fuel')
        truck = request.form.get('truck')
        trailer = request.form.get('trailer')
        isAts = request.form.get('isAts')
        isEts2 = request.form.get('isEts2')
        
        logembed = Embed(
            description="",
            color=0x000000,
            timestamp='now'
        )
        img1 = "https://i.imgur.com/5s6i94n.png"
        hex = "https://i.imgur.com/6JxudkT.jpeg"
        logembed.set_author(name='Hexx', icon_url=hex)
        logembed.set_thumbnail(img1)
        logembed.add_field(name='ID: ', value=id)
        logembed.add_field(name='Date: ', value=date)
        logembed.add_field(name='From: ', value=start)
        logembed.add_field(name='To: ', value=end)
        logembed.add_field(name='Cargo: ', value=cargo)
        logembed.add_field(name='Weight: ', value=f"{weight}T")
        logembed.add_field(name='Distance: ', value=f"{dist}KM")
        logembed.add_field(name='Price: ', value=f"{price}€")
        logembed.add_field(name='Fuel: ', value=f"{fuel}L")
        logembed.add_field(name='Truck: ', value=truck)
        logembed.add_field(name='Trailer: ', value=trailer)
        if isAts == 'Yes':
            logembed.add_field(name='Game: ', value='ATS')
        elif isEts2 == 'Yes':
            logembed.add_field(name='Game: ', value='ETS2')
        
        logHook.send(embed=logembed)
        
        
    return render_template('work_manual.html')
    

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

Share Improve this question asked Jan 11 at 15:24 HexSKHexSK 1 2
  • 2 That's a lot of code... can you narrow it down to a minimal reproducible example? – Mureinik Commented Jan 11 at 17:15
  • pastebin.com/BERsWkvG – HexSK Commented Jan 11 at 22:24
Add a comment  | 

1 Answer 1

Reset to default 0

So the error was that in the lines

start = request.form.get('start')
end = request.form.get('end')

instead of the 'start' and the 'end' value i was supposed to put 'from' and 'to' but i mean it is what it is we fix it

转载请注明原文地址:http://www.anycun.com/QandA/1745706253a91089.html