Pass parameter from REST API call to Server Script API method

I have a Server Script that I am trying to pass a value to from a REST API call.

Using postman, I have tried passing the value as a Param

GET {{methodUri}}GetNewStructure?project=MD-24-4652

as also in the request body

GET {{methodUri}}GetNewStructure
Body: { “project”: “MD-24-4625” }

Using both GET and POST calls

On the server side I try to receive the value:

project = frappe.request.form.get("project");
structure = frappe.new_doc('Detailer Structure');
structure.project = project;
structure.save();
frappe.response['message'] = structure;

The document is returned, but the value is not saved in the document.

Does it work if you use structure.insert() in place of structure.save()? I think that’s the method you want.

See Sagar’s better answer below

1 Like
  • You can’t use GET calls to save data in the database. Ideally you should use POST in this case.

  • If you must continue using GET, you’ll need to commit your changes manually by adding a frappe.db.commit() line after your save() call.

  • Ideally, you don’t need a server script here if you have the flexibility of not using GET. The Framework provides an official way to create new docs using API:

https://docs.frappe.io/framework/user/en/api/rest#create

1 Like