Maintain control of your Microsoft 365 data
Post Reply
Thomasps
Novice
Posts: 5
Liked: 1 time
Joined: Oct 04, 2020 5:34 pm
Contact:

VBO API javascript

Post by Thomasps »

Hi,

First off, I just want to say that I know that this question maybe should have been posted in a different forum but i'm trying here first.

I have already made a Powershell script that gathers information from the API, calculates some stuff and then previews it in a nicely formatted html table :D
Just for fun, I am now trying to make a html with javascript for API call. Im doing things in baby steps here, so I've been able to to collect the token. But when I'm trying to use the token for API calls. I'm just getting this error:
Access to fetch at 'https://localhost:4443/v4/backupreposit ... 90f54e7393' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

To my understanding this can happen because of a couple of diferent reasons, but in my in my situation I think it is happening because the call is coming from a different Port than 4443 (I'm creating the html on the veeam server).

I can see that the first response from Veeam API where I request the token contains "access-control-allow-origin": "*" in the header, but all other responses does not.
Is there something I can add to my javascript to solve this or does anyone know a different way to solve it? I've been trying to read about CORS and to my understanding it's the response from the API that must contain the policy. Is this something that must be solved in the backend?

Here is my simple javascript that I just made with Postman:

Code: Select all

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer Removed token brecause it's length...");

var requestOptions = {
  method: 'GET',
  headers: myHeaders,
  redirect: 'follow'
};

fetch("https://localhost:4443/v4/backuprepositories?proxyid=ef004d36-9ced-4779-92f2-e095f44e7392", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
nielsengelen
Product Manager
Posts: 5797
Liked: 1215 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: VBO API javascript

Post by nielsengelen » 1 person likes this post

The cors error is related to how JavaScript works; I suggest you read up on https://stackoverflow.com/questions/438 ... source-whe which has 3 solutions.

It looks like you are missing the headers like Content-Type: application/json or Accept: application/json in your code.
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen
Thomasps
Novice
Posts: 5
Liked: 1 time
Joined: Oct 04, 2020 5:34 pm
Contact:

Re: VBO API javascript

Post by Thomasps »

Thanks. Im beginning to understand CORS and how it works with javascript now. Ive already been looking into the CORS proxy, but I will first need to learn how to install the proxy on my windows server as the API is not exposed to the internet as well as its not a Linux.

I will try to read up on the 2 other solutions as well.
To my understanding it can be solved with a node.js as well? but im just scratchin the surface of everything here, so I will take some time to read up and start with the two last solutions you provided.


Again, thanks.
nielsengelen
Product Manager
Posts: 5797
Liked: 1215 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: VBO API javascript

Post by nielsengelen »

There are many ways of using Javascript, node.js is just one example for building scaleable web applications.
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests