Friday, November 28, 2025

.::: Simulation API PATCH Curl Using Phyton & MariaDB MySQL with HTTP Status Code 500, 400, 200, 404 :::.

 

continue from https://teguhth.blogspot.com/2025/11/simulation-api-post-using-phyton.html

1. script get sample

[root@teguhth api]# cat patch_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=['PATCH'])
def update_buy_period():
    data = request.get_json()

    # year dan month wajib
    if not data or "year" not in data or "month" not in data:
        return jsonify({
            "MESSAGE": "Bad Request",
            "ERRORS": ["year and month are required"],
            "TRACE_ID": "sim125"
        }), 400

    year = data["year"]
    month = data["month"]

    fields = []
    values = []

    if "start_date" in data:
        fields.append("start_date=%s")
        values.append(data["start_date"])

    if "end_date" in data:
        fields.append("end_date=%s")
        values.append(data["end_date"])

    # Tidak ada field yang di-update
    if not fields:
        return jsonify({
            "MESSAGE": "Bad Request",
            "ERRORS": ["nothing to update"],
            "TRACE_ID": "sim127"
        }), 400

    # Tambahkan filter ke akhir
    values.append(year)
    values.append(month)

    query = f"""
        UPDATE buy_period
        SET {", ".join(fields)}
        WHERE period_year=%s AND period_month=%s
    """

    db = get_db()
    cursor = db.cursor(dictionary=True)
    cursor.execute(query, tuple(values))
    db.commit()

    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 PATCH http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-10"}'
curl -X PATCH 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 PATCH http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-10"}'
{"MESSAGE":"SUCCESS","UPDATED":1}
[root@teguhth api]#

curl -X PATCH 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 PATCH 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":3}
[root@teguhth api]#

 




3. put parameter tidak ada 

curl -X PATCH http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{}'

[root@teguhth api]# curl -X PATCH http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{}'
{"ERRORS":["year and month are required"],"MESSAGE":"Bad Request","TRACE_ID":"sim125"}
[root@teguhth api]#

 




4. put periode tidak ada  


curl -X PATCH http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-10"}'

curl -X PATCH 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 PATCH http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-10"}'
{"ERRORS":["buy period not found"],"MESSAGE":"Not Found","TRACE_ID":"sim126"}
[root@teguhth api]#

[root@teguhth api]# curl -X PATCH 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 PATCH http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-10"}'
curl -X PATCH 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 PATCH http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-10"}'
<!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]#

[root@teguhth api]# curl -X PATCH 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 PATCH http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-10"}'
curl -X PATCH 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 PATCH http://10.10.10.90:8080/update-buy-period -H "Content-Type: application/json" -d '{"year":2025, "month":5, "start_date":"2025-05-10"}'
{"ERRORS":["buy period not found"],"MESSAGE":"Not Found","TRACE_ID":"sim126"}
[root@teguhth api]#
[root@teguhth api]# curl -X PATCH 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

Popular Posts