Regarding Frappe API CAll

I am using frappe API call to call a function but the values are not updating using API call, Can anyone gave me a proper method call API syntax

url1 = “http://localhost:8080/api/resource/Sales Order/” + sales_no + “?runmethod=update_enquiry_status”;
$.ajax({
url: url1,
type: “POST”,
dataType: “json”,
data: { args: JSON.stringify({ prevdoc: quotations[0][‘prevdoc_docname’],
flag: sales_no,})
},
success: function (res2) {
console.log('PUT completed with status ’ + res2.status + url1);

                        },

                        },
             }); //

@3ddeveloper please fix the formatting of your query.

See how the API works in frappe client

Have been struggling with AJAX API POST calls for awhile now, has anyone got this to work? For some odd reason its rejecting the body “data”.

Ive tried the following:

$.ajax({ 
   type: "POST",
   	beforeSend: function (request)
	{
	    request.setRequestHeader("X-Frappe-CSRF-Token", frappe.csrf_token);
	},
   dataType: "json",
   url: "api/resource/Contact",
   data: JSON.stringify({data:{first_name : "John"}}),
   success: function(data){        
     console.log(data);
   }
});

and

$.ajax({ 
   type: "POST",
   	beforeSend: function (request)
	{
	    request.setRequestHeader("X-Frappe-CSRF-Token", frappe.csrf_token);
	},
   dataType: "json",
   url: "api/resource/Contact",
   data: JSON.stringify({first_name : "John"}),
   success: function(data){        
     console.log(data);
   }
});

and many more, all dont seem to work. What is the correct format for data?

Managed to get it working, if anyone else needs to use POST API calls.

Its so strange, it actually requires it to be in query string format even for request.form in app.py.

var data = {“first_name”: “John”};

$.ajax({ 
   type: "POST",
   	beforeSend: function (request)
	{
	    request.setRequestHeader("X-Frappe-CSRF-Token", frappe.csrf_token);
	},
   dataType: "json",
   url: "api/resource/Contact",
   data: "data=" + JSON.stringify(data),
   success: function(data){        
     console.log(data);
   }
});
3 Likes

@bohlian, I’m with the same issue actually, but I’m trying to send a Lead from the WebSite, and I’m unable! Due something very strange with POST + Ajax

@max_morais_dmm, I can only imagine as I have been trying for many months, try the method I proposed, I actually finally got it to work!

It is just very strange that we have to pass data in a “querystring” format but it is actually called using request.form.

FYI It works with child tables as well.

Let me know if you hit problems.

@bohlian are you trying from website too?

@max_morais_dmm, nope, I did it:

  1. from Contact to Contact
  2. Custom App to Stock Entry

both worked. What error did you get?

@bohlian, @max_morais_dmm

Use frappe.call

1 Like

@rmehta, I’m unable to use the frappe.call cause I dont have rights to make an app in the website that I’m doing support! And I’m trying to use API/POST because webforms dont work properly! They are unable of render Link Fields!