0

I'm developing a small program which inserts a data obtained by curl to a MySQL database. Unfortunately, I haven't found any similar about my question, so if you have something on your mind - please point me out there. Here is what I tried so far

import os
import pymysql
from flask import Flask, request
app = Flask(__name__)

db = pymysql.connect("127.0.0.1","meeting_admin","password","rooms")
cur = db.cursor()

@app.route('/book_room', methods = ['POST'])
def book_room():
  try:
   room = request.form['room']
   book_date = request.form['book_date']
   booked_by = request.form['booked_by']
   cur.execute("INSERT INTO booked_rooms(room, book_date, booked_by) 
 VALUES ('%c', '%d', '%s')",(room, book_date, booked_by))
   db.commit()
   cur.close()
   return "OK\n", 200
 except:
   db.rollback()
   return "Smth went wrong", 500

db.close()

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

And the curl command is:

curl -H "Content-type: application/json" -X POST /
http://127.0.0.1:5000/book_room -d / 
'{room=602,book_date:27-08-2019,booked_by="someone"}'

And what I'm getting is:

File "server_flask.py", line 23, in book_room
room = request.form['room']
File "/usr/local/lib/python3.5/dist- 
packages/werkzeug/datastructures.py", line 431, in __getitem__
raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad 
Request: The browser (or proxy) sent a request that this server could 
not understand.

Thanks in advance.

KT1M
  • 23
  • 8
  • One obvious issue is that your payload doesn't seem to contain valid JSON. Are you getting a `JSONDecodeError`? – ChrisGPT was on strike Aug 30 '18 at 15:16
  • Yes, completely forgot about the error output. Question has been updated. – KT1M Aug 30 '18 at 15:18
  • Possible duplicate of [InterfaceError (0, '')](https://stackoverflow.com/questions/6650940/interfaceerror-0) – ChrisGPT was on strike Aug 30 '18 at 15:22
  • Question has been updated. – KT1M Aug 30 '18 at 15:22
  • 1
    Proposed duplicate isn't fully correct, because it's uses Django and in my case data is passing to the server by curl. – KT1M Aug 30 '18 at 15:27
  • It actually looks like a bad request: I don't see the `InterfaceError` in your question anymore. As I mentioned earlier, your JSON doesn't look valid. Try sending `'{"room": 602, "book_date": "27-08-2019", "booked_by": "someone"}'` instead. (I'm also not sure that your date format will be parsed correctly, but let's tackle the invalid JSON first.) – ChrisGPT was on strike Aug 30 '18 at 15:28
  • In fact I'm just trying to pass the data in the form which server could understand, so it might be not JSON actually. I simply have no idea how to correctly pass values for the MySQL... values inside of Python code. – KT1M Aug 30 '18 at 15:31
  • 2
    I don't think MySQL has anything to do with this. It's failing sooner than that, and your inconsistent use of `:` and `=` in the request is unlikely to work, regardless of what format the server is expecting. Upon closer inspection it looks like your server wants form encoded data, which means something like `curl ... -d "room=602&book_date=27-08-2019&booked_by=someone"` or possibly using `-F` instead of `-d`. Alternatively you could update your Flask server to expect a JSON request. – ChrisGPT was on strike Aug 30 '18 at 15:35
  • @Chris you are right. `-d, --data DATA HTTP POST data`, `-F, --form CONTENT Specify HTTP multipart POST data` – Danila Ganchar Aug 31 '18 at 13:46

0 Answers0