Answer the question
In order to leave comments, you need to log in
Why do I get a 404 error when I send a POST request to the server?
My task is to send a request to the server with two parameters login and password and parse the response in JSON format, which it will return to me.
I have a custom script running on the server:
import sqlite3
from flask import Flask, jsonify, request, abort
import json
app = Flask('__name__')
def register(login, password, data=""):
insert_data = (login, password, data)
conn = sqlite3.connect("users.db")
cursor = conn.cursor()
x = cursor.execute(" SELECT * FROM users WHERE login=?", (login,)).fetchall()
if not x:
cursor.execute("INSERT INTO users VALUES (?,?,?)", insert_data)
conn.commit()
return "OK"
return "Username is registered"
def login(login, password):
insert_data = (login, password)
conn = sqlite3.connect("users.db")
cursor = conn.cursor()
x = cursor.execute(" SELECT data FROM users WHERE login=? AND password=?", (login,password)).fetchone()
if not x:
return "Login or password is incorrect"
return {"status": "logged in", "data": x[0]}
def delete(login, password):
insert_data = (login, password)
conn = sqlite3.connect("users.db")
cursor = conn.cursor()
x = cursor.execute(" SELECT data FROM users WHERE login=? AND password=?", (login,password)).fetchone()
if not x:
return "Login or password is incorrect"
x = cursor.execute(" DELETE FROM users WHERE login=? AND password=?", (login,password)).fetchone()
conn.commit()
return "user deleted"
#=======================================================================================
@app.route('/register', methods=['POST'])
def write_reg():
if not request.json:
print(request.json)
abort(404)
user = {"login": request.json["login"],
"password": request.json["password"],
"data": request.json.get("data") or ""}
return register(**user), 201
@app.route('/login', methods=['POST'])
def write_log():
if not request.json:
print(request.json)
abort(404)
user = {"login": request.json["login"],
"password": request.json["password"]}
return login(**user), 201
@app.route('/delete', methods=['POST'])
def write_del():
if not request.json:
print(request.json)
abort(404)
user = {"login": request.json["login"],
"password": request.json["password"]}
return delete(**user), 201
@app.errorhandler(404)
def not_found(error):
return jsonify({'error': 'Not enought data'}), 404
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True, threaded=True, port=5123)
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + "?" + Data);
request.Method = "POST";
request.Accept = "application/json";
request.UserAgent = "Mozilla/5.0....";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
StringBuilder output = new StringBuilder();
output.Append(reader.ReadToEnd());
response.Close();
System.Net.WebException: "Удаленный сервер возвратил ошибку: (404) Не найден."
HTTP/1.0 201 CREATED
Content-Length: 43
Content-Type: application/json
Date: Mon, 07 Sep 2020 09:49:10 GMT
Server: Werkzeug/0.16.0 Python/3.7.7
{
"data": "",
"status": "logged in"
}
Answer the question
In order to leave comments, you need to log in
404 rushes at
if not request.json:
print(request.json)
abort(404)
var httpClient = new HttpClient();
var request = new
{
login = "admin",
password = "12345"
};
var jsonString = JsonSerializer.Serialize(request);
var response = await httpClient.PostAsync(url, new StringContent(jsonString))
var contentString = await response.Content.ReadAsStringAsync();
var result = JsonDocument.Parse(contentString);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question