Curl for Rest Api

Can we use a curl request for testing the rest api in the frappe…
If yes How can we do it…Please some examples…I already tried the api/resource/Doctype …BUt it is giving the 403 error

I will suggest to use either

  1. Postman: https://www.getpostman.com/
  2. Httpie : https://httpie.org/

About your examples
https://frappe.github.io/frappe/user/en/guides/integration/rest_api.html

dont forget to Login first.

1 Like

I’m trying to use a curl request for testing as well but having some trouble logging in.

Should this work?

curl -d "usr=EMAIL&pwd=PASSWORD" http://MYSITE.com/api/method/login

Could you give an example of a cURL request to Login?

Hi, try out these.
Login -

curl -d “usr=Administrator&pwd=admin” http://0.0.0.0:8003/api/method/login -c frappe.cookie

-c to save cookie in file, -b to use the cookie in further requests

Get request -

curl http://0.0.0.0:8003/api/resource/ToDo -b frappe.cookie

Post request

curl -H “Content-Type: application/json” -d ‘{“description”:“Test Rest”}’ http://0.0.0.0:8003/api/resource/ToDo -b frappe.cookie -X POST

refer REST API url above for more. Thanks

3 Likes

Confirming these examples worked.

Thanks so much!

Thanks for the examples on using Curl. I’ve successfully duplicated your results for Authentication and GET. I’m still working on POST, but I’m sure I will figure it out.

However, what I find strange is when I submit an invalid URI. I would expect to receive an HTTP 404 or 501. But instead, the API returns the entire HTML of the main page.

For example, assume we try this:

curl http://192.168.0.5:8000/api/resource/Customerz -b frappe.cookie

Notice I’ve misspelled the resource name, typing Customerz instead of “Customer”. When I submit this command, instead of an error, I receive back the entire main web page:

`!DOCTYPE html>

Not Found ..... .... `

Does this happen for everyone else? Is this just how the REST API was designed, to default bad requests?

Thanks much for any thoughts/ideas!
~Brian

Hi @brian_pond,
you are right the http status code for resource not found handled by frappe is 200. Can’t rely on status code? I’m not sure will let you know if I figure something.
BTW, you are seeing the body of the response, pass -I as a parameter just to see the header.

Thanks

No need, Ranjith. I was just surprised to learn the Route handler is returning the HTML of the main page as a response. When the request is bad, I expected it to return either nothing. Or an empty json string. Or some kind of message. Instead, whenever there is a bad request, the route handler is returning the default route of the whole site…which is the main page.

Not ideal. But nothing we cannot work around. As you said, first handle the status codes, and then pay attention to the body of the response.