Cannot instantiate a process with axios via HTTP
I have a Node.js API which I'm trying to communicate with Bonitasoft via Axios, I can login and get the processes that the logged user can instantiate, no problem there. But when I want to instantiate a process I'm getting 401 code, I'm passing the bonita token in the header as:
X-Bonita-API-Token: Token value from login
Also tried sending the cookie header as:
Cookie: bonita.tenant=1; SameSite=Lax,JSESSIONID=JSSSIONID form login; Path=/bonita; HttpOnly; SameSite=Lax,X-Bonita-API-Token=Token value from login; Path=/bonita; SameSite=Lax,BOS_Locale=es; Path=/; SameSite=Lax
But I still get the 401 error, the user can instantiate the process, he is the initiator of it, and I can do it on the bonita portal, but can't get it working with axios.
How can I achieve this?
Hi! Of course, this is the code I use to authenticate to bonita
const bonitaLogin = await axios({
method: "post",
url: `${process.env.BONITA_URL}bonita/loginservice`,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
data: qs.stringify({
username: username,
password: password,
}),
})
.then((data) =>
this.getLoginDataFromString(data.headers["set-cookie"]!.toString())
)
Where getLoginDataFromString is
static getLoginDataFromString(token: string) {
return {
JSESSIONID: token.substring(
token.indexOf("JSESSIONID=") + "JSESSIONID=".length,
token.indexOf(";", token.indexOf("JSESSIONID="))
),
"X-Bonita-API-Token": token.substring(
token.indexOf("X-Bonita-API-Token=") + "X-Bonita-API-Token=".length,
token.indexOf(";", token.indexOf("X-Bonita-API-Token="))
),
};
}
And then you can make requests like this:
await axios({
method: "get",
url: `${
process.env.BONITA_URL
}bonita/API/bpm/process?c=1000&s=&o=displayName%20ASC&f=activationState=ENABLED${
userId ? `&f=user_id=${userId}` : ""
}`,
headers: {
Cookie: `X-Bonita-API-Token=${token}; Path=/bonita;JSESSIONID=${sessionId}; Path=/bonita; HttpOnly;`,
"X-Bonita-API-Token": token,
},
})
Hi juanp2299! sorry to bother you. Would you be so kind to share with me the code in which you perform the authentication with Bonita and Node js? I'm getting a 401 error when trying to authenticate and I can't figure out what the problem is.
Thank you!
Comments
Hi! Of course, this is the code I use to authenticate to bonita
const bonitaLogin = await axios({
method: "post",
url: `${process.env.BONITA_URL}bonita/loginservice`,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
data: qs.stringify({
username: username,
password: password,
}),
})
.then((data) =>
this.getLoginDataFromString(data.headers["set-cookie"]!.toString())
)
Where getLoginDataFromString is
static getLoginDataFromString(token: string) {
return {
JSESSIONID: token.substring(
token.indexOf("JSESSIONID=") + "JSESSIONID=".length,
token.indexOf(";", token.indexOf("JSESSIONID="))
),
"X-Bonita-API-Token": token.substring(
token.indexOf("X-Bonita-API-Token=") + "X-Bonita-API-Token=".length,
token.indexOf(";", token.indexOf("X-Bonita-API-Token="))
),
};
}
And then you can make requests like this:
await axios({
method: "get",
url: `${
process.env.BONITA_URL
}bonita/API/bpm/process?c=1000&s=&o=displayName%20ASC&f=activationState=ENABLED${
userId ? `&f=user_id=${userId}` : ""
}`,
headers: {
Cookie: `X-Bonita-API-Token=${token}; Path=/bonita;JSESSIONID=${sessionId}; Path=/bonita; HttpOnly;`,
"X-Bonita-API-Token": token,
},
})
Comments
Here is my code that I use to Instantiate a Process. Mostly Fetch API works. I have trouble with HTTP.GET or HTTPCLIENT
public StartProcess(processId:String,token:any,contractInput:any){
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
console.log(this.getToken());
myHeaders.append("x-bonita-api-token",JSON.parse(localStorage.getItem('API-Token') || '{}'))
return fetch('http://localhost:8080/bonita/API/bpm/process/'+ processId + '/instantiation',{
method: 'POST',
headers:myHeaders,
credentials: 'include',
body:JSON.stringify(contractInput)
}).then(res=> res.json())
.then(data=>{ return (data);})
.catch(err => { return err})
}