How to call External Web Service

Hello All,
How to call external web service in erpnext.
please give me your valuable help

Thank and regards

Hello @vivek22793. I’m trying to work with a library called zeep. this allows me access to soap services. however I do have challenges configuring for a service that is on https under ERPnext

Thanks for your reply.
Sir i solved this problem.
And its working now.

Hi @vivek22793, i’m new to erpnext, can u please explain in steps how to do that. If anyone know the solution please help me, that will be more helpful for me.

Ya sure, can you tell me what you want do? case?

Thanks @vivek22793 for the reply.
Case: When i make sales order using purchase order, once i click submit button it should send the sales order details in json format to external api. This is my use case. I hope you will understand.

Yes i understood your scenario. I solved this problem using python library (requests)
requests.post ("url of your external service ", data= json.dumps(your data))
for eg.
import requests
head= {“Accept”:“applicaiton/json”, “Content-type”: “application/json”}
data= {
“name” : self.name,
“login_id” : self.name,
“password” : self.password,
}
response= requests.post( http://192...*:0000/service/, data= json.dumps(post_data))

Whatever u sent i understood, now the thing is the sales order details have many fields, from that i need some particular fields which i need to send as payload, can u please help me out for this case. Thanks u so much for quick response.

you should also try webhook

Create dictionary and bind data accordingly.
like for eg.
{
“customer” : self.customer,
“transaction_date”: self.transaction_date,
“item_details”: [{}, {}…]
}

Thank you, I’ll do this.

Yes sure and let me know if any issue.

sure, thank you

@vivek22793 i got this error when i run this code. url is just sample url

@frappe.whitelist()
def post_so_to_retailer(so_detail, so):
url = ‘http://dummy.restapiexample.com/api/v1/employees
payload = {
“Customer”:so_detail.customer,
“Items”:[{
“Code”:so_detail.item_code
}]
}
headers = {‘content-type’: ‘application/json’}
print(payload)
r = requests.post(url, data=json.dumps(payload), headers=headers)
return r

Traceback (most recent call last):
File “/opt/bench/erpnext/apps/frappe/frappe/app.py”, line 61, in application
response = frappe.handler.handle()
File “/opt/bench/erpnext/apps/frappe/frappe/handler.py”, line 21, in handle
data = execute_cmd(cmd)
File “/opt/bench/erpnext/apps/frappe/frappe/handler.py”, line 56, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “/opt/bench/erpnext/apps/frappe/frappe/init.py”, line 1036, in call
return fn(*args, **newargs)
File “/opt/bench/erpnext/apps/erpnext/erpnext/selling/doctype/sales_order/sales_order.py”, line 991, in post_so_to_retailer
“Customer”:so_detail.customer,
AttributeError: ‘str’ object has no attribute ‘customer’

whenever you send data using args it convert all data type as string and send the data
for eg. you are sending data as {“customer”: doc.customer} so your data will go like “{“customer”: doc.customer}”’
so you have to use python eval function.

@frappe.whitelist()
def post_so_to_retailer(so_detail, so):
so_detail= eval(so_detail)
url = ‘http://dummy.restapiexample.com/api/v1/employees’
payload = {
“Customer”:so_detail.customer,
“Items”:[{
“Code”:so_detail.item_code
}]
}
headers = {‘content-type’: ‘application/json’}
print(payload)
r = requests.post(url, data=json.dumps(payload), headers=headers)
return r

Thank you @vivek22793 , i already got solution for that, i have done the same thing that u mentioned.
I’ll connect back if i get any issues, thank you once again.

Mark solution and close down the topic :slight_smile:

Hello @D_N_Srinath

where is this error coming from (the traceback call) I’m new to erpnext and have made some changes in my code. But I can’t see where the logs are coming for these files.
Can you tell me the location of log files or the console so I can debug the code??

These challenges must not be unique to ERPNext.
At its core, ERPNext is based on Werkzeug. Anything that is supported by Werkzeug can definitely be done in Frappe.
There are multiple examples in the frappe/Erpnext codebase itself on how to do the same, you can checkout twillio, webhooks, etc. implementation to understand the approach

The logs are stored in logs folder (Check suitable file based on use case).
Also there is this doctype: /desk#List/Error%20Log/List where errors also go to