actopozipc
actopozipc

Reputation: 105

How to add CORS header to my REST API if I dont have a domain?

I am working on a REST API and it runs on an extern server with an own public IP. Now I want to call it from my PC at home, which logically doesnt have an own IP or domain. How am I supposed to set the Access-Control-Allow-Origin for my PC?

This is my function (I censored the API key):

async function loadTelluzApi(url) {

            const response = await fetch(url, {
                method: 'GET',
              //  body: myBody, // string or object
                headers: {
                    'ApiKey': 'apikey'
                }
            });
             console.log(response.json())
            return await response.json();

    }

For test-purposes I added this above the controler of my REST API:

[EnableCors(origins: "*", headers: "*", methods: "*")]

And I get the following error:

Reason: CORS header 'Access-Control-Allow-Origin' missing

Upvotes: 0

Views: 216

Answers (1)

actopozipc
actopozipc

Reputation: 105

I also had to add the custom-headers in the Web.config file:

<httpProtocol>
        <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*" />
          <add name="Access-Control-Allow-Headers" value="Content-Type" />
          <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
         <add name="Access-Control-Allow-Credentials" value="true" />
        </customHeaders>
      </httpProtocol>

Upvotes: 1

Related Questions