Currently, I am making a POST request to the Rest API but I wanted to use the OAuth2 Access Token. How will I use the access token? Currently, this is the code of my POST request.
const promise = z.request({
url: `https://----------/`,
method: 'POST',
body: JSON.stringify({
description: bundle.inputData.description
}),
headers: {
'content-type': 'application/json'
}
});
return promise.then(response => JSON.parse(response.content));
Use Authorization Header to send access token.
For how to setup and use oauth refer : https://frappe.io/docs/user/en/guides/integration
const promise = z.request({
url: `https://----------/`,
method: 'POST',
body: JSON.stringify({
description: bundle.inputData.description
}),
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer <access_token>'
}
});
return promise.then(response => JSON.parse(response.content));
1 Like
Thank you @revant_one . Let me try this one.
Hello @revant_one . I have already made it work for GET request but it doesn’t work on POST request. This is the code that I have tested with.
import requests
headers = { 'Authorization': 'Bearer ----' }
# GET request
r = requests.get('https://---/api/resource/ToDo', headers=headers)
print(r) # [Returns 200]
# POST Request
payload = { 'content-type': 'application/json',
'description': 'This is a simple todo.' }
r = requests.post('https://---/api/resource/ToDo', headers=headers, data=payload)
print(r) # [Returns 500]
Thank you for helping me.
convert payload json to string
try json.dumps(payload) while making the request.
and json.loads(frappe.request.data) when reading data from request.
e.g.
make request
r = requests.post(url, data=json.dumps(doc))
read request
@frappe.whitelist(allow_guest=True)
def post_test():
return json.loads(frappe.request.data)
1 Like
Thank you @revant_one for helping me. The team and I fixed the problem together with the permission problems from Response 500, and Response 403