ERPNext Foundation ERPNext Cloud User Manual Blog Discuss Frappé* Donate

How to call External Web Service

customization

#1

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

Thank and regards


#2

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


#3

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


#4

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.


#5

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


#6

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.


#7

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))


#8

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.


#9

you should also try webhook


#10

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


#11

Thank you, I’ll do this.


#12

Yes sure and let me know if any issue.


#13

sure, thank you


#14

@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’


#15

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


#16

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.


#17

Mark solution and close down the topic :slight_smile: