I want to create the api for this form. The scenario is, i have created this doctype directly on the server so that my client can access it for testing.
Please can someone tell me the path how can i create the api for this doctype.
Hi @Aditya_vig1:
REST API is created automatically for your doctype. Look here:
https://frappeframework.com/docs/user/en/api/rest
You can create also custom api:
Hope this helps.
Thanks @avc for replying.
Please have a look into this. and tell me where i am wrong
frappe.ui.form.on(‘Material Tagging’, {
// refresh: function(frm) {
fetch(‘http://0.0.0.0:8000/api/method/erpnext.rfid_automation.doctype.material_tagging.api.add_custom_data’, {
headers: {
‘Authorization’: ‘token 925e0e81ae34590:a0aaade8e4d9ed2’
}
})
.then(r => r.json())
.then(r => {
console.log(r);
})
// }
});
this is the js code
mport frappe
import json
@frappe.whitelist()
def add_custom_data():
try:
# Get the JSON data from the request
data = frappe.request.data
print(data)
# json_data = frappe.parse_json(data)
json_data = frappe.parse_json(data.decode('utf-8'))
# Check if the data is empty or if "data" key is not found
if not json_data or "data" not in json_data:
return {'message': 'No valid data provided in the request.'}
# Define the DocType name
doctype_name = 'Material Tagging' # Replace with the actual DocType name
# Access the "data" key to get the list of dictionaries
data_list = json_data["data"]
# Create and insert entries from the list of dictionaries
for entry_data in data_list:
# Create a new document of the custom DocType
doc = frappe.get_doc({
'doctype': doctype_name,
'batch': entry_data.get('batch'), # Use the correct field names
'customer': entry_data.get('customer'), # Use the correct field names
'date': entry_data.get('date'),
'shade': entry_data.get('shade'),
'fabric_width': entry_data.get('fabric_width'),
'style': entry_data.get('style'),
'roll': entry_data.get('roll'),
'roll_weight': entry_data.get('roll_weight'),
'yard': entry_data.get('yard'),
'manufacturer': entry_data.get('manufacturer'),
'rfid_tag': entry_data.get('rfid_tag'),
# Add other fields as needed
})
# Save the document
doc.insert()
return {'message': f'{len(data_list)} entries added successfully'}
except Exception as e:
frappe.log_error(frappe.get_traceback(), 'Custom Data API Error')
return {'message': str(e)}
this is the python code.
in local i am able to hit this api with postman but when i am on server and hit this api as the url
Please tell me what i am doing wrong??
HI @Aditya_vig1:
Maybe I am not understanding your requirement … but:
Use frappe.call to get access to the resources from client side (inside frappe application). No authentication headers needed here.
https://frappeframework.com/docs/user/en/guides/basics/frappe_ajax_call
To use REST API from external apps, follow the instructions mentioned above:
https://frappeframework.com/docs/user/en/api/rest#create
POST REQUEST
http://127.0.0.1:8010/api/resource/Material%20Tagging
HEADERS
{
"Accept": "application/json",
"Content-Type": "application/json",
}
AUTHENTICATION
Add HEADERS
'Authorization': 'Basic api_key:api_secret'
Note that token (api_key:api_secret) should be Base64 encoded
BODY
{"batch": batch,
"customer": customer,
"date": date,
}
Hope this helps.
Hi @avc
Can i tell you my requirement agian.
I just have to give this api to my client who is not using this frappe, erpnext. They will access it on their application. I just have to give the end-point to my client. They should be able to access the fields on this doctype and enter the values and post it.
How can they access the fields which are present on this doctype?
Thanks & Regards
Hi @Aditya_vig1:
endpoint:
http://yoursite.com/api/resource/Material%20Tagging
Please, read this. All operations (create, update, delete …) over all your doctype data are avaliable:
https://frappeframework.com/docs/user/en/api/rest#crud-operations