Hi I am new to working with the Jenkins API, and I want to know why my current implementation is not working with the fetch API. I have tested its implementation with postman, and it works fine, however, when I use the fetch API in a node vue.js project, I getting a CORS error when I set the request mode to use cors, and a 403 forbidden error when I am using the no-cors mode. Here is my code that I currently have for my implementation:
fetch("https://<domain-url>/crumbIssuer/api/json",{
method:'GET',
mode: 'cors', //<=== cors request mode set here
headers:{
'Authorization': 'Basic <hashed username and password for jenkins basic authentication>',
'Content-Type': 'application/json',
},
}).then(res=>{
if(res.ok){
return res.json();
}else{
....
}
}).then(data=>{
....
}).catch(()=>{
.....
});
If I use this code, I get a CORS error as shown in the following image: Jenkins CORS Error However, I do suspect that this can be fixed via the implementation of a proxy. Although, I would like to know if my fetch was just simply misconfigured in some way, such as the URL, headers, etc.
NOTE: I am using the exact same URL on my postman implementation: Setup in postman
Also, I tried setting the request mode to no-cors and got a 403 error: 403 error However, according to the article that I have linked below, setting the request mode to no-cors is not a good idea since it prevents front-end javascript code from reading the response body. Here is a link to that article:
Trying to use fetch and pass in mode: no-cors
I'm not sure why it returns a 403 forbidden error when in no-cors though.
I'm really thinking that a proxy server could solve this issue, but any other suggestions would be great. (Or pointing out a stupid mistake) thanks!