How to return custom json response

Hi,
How can i return custom json response to clients that consume my API ?

You can convert any python method into a method that handles your API calls.

Create a method in a python file in your custom app:

custom_app/api/utils.py

@frappe.whitelist()
def ping():
  return 'pong'

Call that method from your client side:

fetch('/api/method/custom_app.api.utils.ping')
  .then(r => r.json())
  .then(r => console.log(r.message)) // 'pong'

More at REST API

1 Like

Thanks for your reply. How can i add a response status with the json data ? 200, 201 400 etc …

@frappe.whitelist
def ping():
try:
# some statement…
return {“response_status”:200,“data”:response}
except:
return {“response_status”:404,“message”:“Data not found!”}

Thanks for your reply,
is there a way to trigger the frappe.throw function to return json DATA because if i wante to return error json response i need to cascad return from within the specific function the error occurred