Handling API responses recieved on server scripts

Hi,

I have no access to backend of the ERPNext. I am subscribed to the $10/m plan. I have been using server/client scripts to add several features as I need.

My question today is regarding the Response object I recieve when I make the following get call (from server script of a doctype) to a service outside of frappe.

resp = frappe.make_get_request(url, headers=header)
frappe.response["data"] = resp
doc.raw_response = frappe.response["data"]
doc.raw_text = str(resp)

Now, doc.raw_response is a JSON fieldtype, and raw_text is Text.

Using the above, I get an empty dictionary (just → {}) in both the fields, while it should give me this → {“ABC.XYZ”:[{"Vendor No.: ":“XYZ”,"Start Date: ":“03/14/23”,"End Date: ":“03/15/23”,“Error”:“No materials”}]}

I get the correct response if I use Postman or even a local python script. Appreciate if someone could help resolve this, or provide a reference to make this work.

hi,

what is the response in the console?

Hi @ayah,

Here’s the console output with the above script change.

hi,
check the header’s content in console:

frappe.errprint(header)
resp = frappe.make_get_request(url, headers=header)
frappe.response[“data”] = resp
doc.raw_response = frappe.response[“data”]
doc.raw_text = str(resp)

@ayah, I set the header in the script as the following, and thats what I see on the console. I still attached the screenshot you requested.

url = ‘https://{client_url}/’
header = {‘Content-Type’: ‘application/json’}
frappe.errprint(header)
resp = frappe.make_get_request(url, headers=header)
frappe.errprint(resp)
frappe.response[“data”] = resp
doc.raw_response = frappe.response[“data”]
doc.raw_text = str(resp)

I feel if this is because of the “frappe.make_get_request” method. Maybe there is another way of decrypting the “response object” I receive on frappe.

hi,

the response from your service outside of frappe is “{}” in console log

i’ve created server script as follow:

client_url = ‘mytesterserver.free.beeceptor.com/my/api/path
url = f’https://{client_url}’
header = {“Content-Type”:“application/json”}
resp = frappe.make_get_request(url, headers=header)
frappe.errprint(resp)
frappe.msgprint(resp[“status”])

It works

@ayah ,

Firstly, I appreciate the efforts you are taking to help me solve this. I wanted to provide a detailed situation regarding my current challenge.

I have tested my client’s URL using Postman and a simple Python script utilizing the request library (outside erpnext). Both of these methods can successfully receive the JSON response. Here is an example response I received from both:

response.json()
{“ABC.XYZ”:[{"Ven No.: ":“XYZ”,"Start Date: ":“03/14/23”,"End Date: ":“03/15/23”}]}

response.headers
{‘Content-Type’: ‘text/plain; charset=utf-8’, ‘Date’: ‘Wed, 15 Mar 2023 14:03:14 GMT’, ‘Content-Encoding’: ‘gzip’, ‘Transfer-Encoding’: ‘chunked’, ‘Vary’: ‘Accept-Encoding’, ‘Request-Context’: ‘appId=zzzz-zzzz-zzzz-zzzz’}

However, when attempting to run the same script through the erpnext server scripts, the response I receive is only an empty object {}.

I have double-checked my URLs and parameters several times, and they are identical to the ones I used in Postman and the Python script.

Furthermore, I tested using a dummy server to respond to the server script, and it worked flawlessly. Therefore, I am confident that the server script also functions correctly.

So the server script particularly doesn’t want to work for my clients url… haha. Are there any other methods I could achieve the result, like through client scripts or using other intermidiate platform?

hi

using client script:

frappe.ui.form.on(‘Purchase Order’, {
after_save(frm) {
fetch(‘https://mytesterserver.free.beeceptor.com/my/api/path’, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’
}
})
.then(r => r.json())
.then(r => {
console.log(r[“status”]);
})
}
})

The Client script above does not work for me. It says syntax error while loading the document. And it also doesnot show where that error is. I tried all possible syntax corrections, I think “fetch” is not the correct command for outside calls. Did it work for you @ayah ?

hi,

yes it worked

client script

console log

@ayah,
After implementing the same code to the same doctype, I encountered an error while attempting to start a new PO, which is depicted in the attached screenshot.

However, when saving the PO, the document is saved correctly, and the console does not log anything.

I am uncertain whether the script you provided is being executed at all. Could you please review my implementation and advise if there is an issue that needs to be addressed?

hi

try change the client script as follow:

client script v2

and check the console
console log v2

@ayah ,

It works for your url. Although I get the following error for the url I need to talk to.

From the client side you can call API Server script using regular frappe.call function like below

frappe.call({
        	         	"method": "get_some_server_api_function",
                 		"args": {

                		    "arg1":value_arg1, 
                            "arg2":value_arg2,

                 		},
                		"callback": function(response) {
                		    if(response.message) {
                                       console.log(response.message)
                                   }
                              }
.
.
.