continue from https://teguhth.blogspot.com/2025/11/simulation-api-post-using-phyton.html
1. script get sample
[root@teguhth api]# cat /data/api/put_api_buy.py
from flask import Flask, request, jsonify
import mysql.connector
app = Flask(__name__)
def get_db():
return mysql.connector.connect(
host="localhost",
user="admin",
password="admin",
database="hris"
)
@app.route('/update-buy-period', methods=['PUT'])
def update_buy_period():
data = request.get_json()
# Validasi body JSON
if not data or "year" not in data or "month" not in data or \
"start_date" not in data or "end_date" not in data:
return jsonify({
"MESSAGE": "Bad Request",
"ERRORS": ["missing required fields"],
"TRACE_ID": "sim125"
}), 400
year = data["year"]
month = data["month"]
start_date = data["start_date"]
end_date = data["end_date"]
db = get_db()
cursor = db.cursor(dictionary=True)
cursor.execute("""
UPDATE buy_period
SET start_date=%s, end_date=%s
WHERE period_year=%s AND period_month=%s
""", (start_date, end_date, year, month))
db.commit()
# Jika tidak ada row yang ter-update ? data tidak ditemukan
if cursor.rowcount == 0:
return jsonify({
"MESSAGE": "Not Found",
"ERRORS": ["buy period not found"],
"TRACE_ID": "sim126"
}), 404
return jsonify({
"MESSAGE": "SUCCESS",
"UPDATED": cursor.rowcount
}), 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
[root@teguhth api]#
2. put berhasil
curl -X PUT http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-05", "end_date":"2025-05-29"}'
[root@teguhth api]# curl -X PUT http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-05", "end_date":"2025-05-29"}'
{"MESSAGE":"SUCCESS","UPDATED":1}
[root@teguhth api]#
3. put parameter tidak ada
curl -X PUT http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{}'
[root@teguhth api]# curl -X PUT http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{}'
{"ERRORS":["missing required fields"],"MESSAGE":"Bad Request","TRACE_ID":"sim125"}
[root@teguhth api]#
4. put periode tidak ada
curl -X PUT http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-05", "end_date":"2025-05-29"}'
[root@teguhth api]# curl -X PUT http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-05", "end_date":"2025-05-29"}'
{"ERRORS":["buy period not found"],"MESSAGE":"Not Found","TRACE_ID":"sim126"}
[root@teguhth api]#
5. put table tidak ada
curl -X PUT http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-05", "end_date":"2025-05-29"}'
[root@teguhth api]# curl -X PUT http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-05", "end_date":"2025-05-29"}'
<!doctype html>
<html lang=en>
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
[root@teguhth api]#
6. put table ada tapi data kosong
curl -X PUT http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-05", "end_date":"2025-05-29"}'
[root@teguhth api]# curl -X PUT http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-05", "end_date":"2025-05-29"}'
{"ERRORS":["buy period not found"],"MESSAGE":"Not Found","TRACE_ID":"sim126"}
[root@teguhth api]#
















No comments:
Post a Comment