Ravindu Sachintha
Ravindu Sachintha

Reputation: 141

Angular 9 project on firebase hosting failed to pass https requests

I have use a proxy on my angular project as below.

{
  "/api": {
    "target" : "https://<domain_name>/",
    "secure": false
  }
}

it is perfectly worked in locally with ssl for HTTPS POST requests by executing the command of,

ng serve --ssl true --proxy-config proxy.conf.json

But after I host it to the firebase hosting, it always gives below error on every POST request.

Http failure during parsing for https://<app_name>.firebaseapp.com/api/<route_name>

Upvotes: 2

Views: 1469

Answers (1)

Shravan
Shravan

Reputation: 1202

The proxy config file you are including while running the Angular app in local environment to redirect certain URL segments is a feature provided by the Angular dev server.

Since you are using firebase, firebase also provides a similar redirecting feature that can be configured in the firebase.json file. Since you are using firebase for deployment, I assume you already have a firebase.json file. If not, visit Firebase CLI official docs to install it in your local environment. Now running firebase init command will generate a basic firebase.json file.

Include the redirect option as shown in the example below:

firebase.json

"hosting": {
  // ... other configurations ...
  "redirects": [ {
    "source": "/api/:path*",
    "destination": "https://<gcp_domain_name>/:path",
    "type": 301
  }
}

Let's breakdown what we just did. The source property takes a URL segment as value for Firebase to perform redirection. :path* specifies that any URL segment after /api should captured and stored in the variable :path. Now in the destination property, specify the destination domain followed by /:path to perform the redirect. The type property specifes the Http Response code 301 to represent the permanent redirect (in your case). You can add more than one redirection in the redirects array.

For more information on the redirection configuration in Firebase, refer this page from Firebase official docs. In addition to redirection, you can find all options that can be configured in Firebase hosting.

Upvotes: 5

Related Questions