How to setup the proxy?

Hi guys, I am having a CORS problem for certain services that I am calling (please, notice that I don’t have access to the code of these services so I can’t modify them or anything). I’ve read the problem is about CORS since it’s working on my device but not on the browser with ionic serve and Angular’s HttpClient. This is the error I am getting:

Http failure response for (unknown url): 0 Unknown Error

I’ve read that setting a proxy can fix this but I don’t seem to be able to do it. I don’t know if I’m settig it up wrong or it just won’t work even with a proxy.

This is how my proxy in ionic.config.json is:

"proxies": [
    {
      "path": "/*",
      "proxyUrl": "https://hostwhereservicesare.net/*"
    }
  ]

I want that all the calls I make to the domain in proxyUrl don’t give this problem so idk if the /* part is right.

I repeat this only happens in the web with Angular HttpClient and ionic serve. In my device I use the Ionic Native HTTP and it works perfectly. But it makes it a pain in the ass and waste of time to test and debug on the device.

Any idea on how to do this?

Thanks.

Follow the documentation on service proxies:


/* doesn’t work, use /service or something like that.

Hi, I already followed it and tried many ways but it doesn’t seem to work. Idk if because I’m doing something wrong.

Also, I don’t understand why it’s giving CORS problem since I receive the following headers:

Access-Control-Allow-Headers →authorization,Access-Control-Allow-Origin,Content-Type
Access-Control-Allow-Methods →HEAD,DELETE,POST,PATCH,PUT,GET
Access-Control-Allow-Origin →*
Cache-Control →no-cache, no-store, max-age=0, must-revalidate
Connection →keep-alive
Content-Type →application/json
Date →Thu, 30 Aug 2018 08:32:43 GMT
Expires →0
Pragma →no-cache
X-Content-Type-Options →nosniff
X-Frame-Options →DENY
X-XSS-Protection →1; mode=block
transfer-encoding →chunked

If you want to debug your CORS problem, open a new topic and describe the actual error message.

If you want to debug your proxy problem, show a configuration that follows the documentation and that doesn’t work.

What is your ionic info output?

Hi, the configuration for the proxy is in the 1st post but I’ve changed it to:

"proxies": [
    {
      "path": "/videos-explicativos",
      "proxyUrl": "https://myserver.net/videos-explicativos"
    }
  ]

My Ionic info is:

Ionic:

   ionic (Ionic CLI)  : 4.1.1 (C:\Users\UsuarioDT01\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework    : ionic-angular 3.9.2
   @ionic/app-scripts : 3.1.10

Cordova:

   cordova (Cordova CLI) : not installed
   Cordova Platforms     : not available
   Cordova Plugins       : cordova-plugin-ionic 4.1.7, cordova-plugin-ionic-keyboard 2.1.2, cordova-plugin-ionic-webview 2.0.2, (and 12 other plugins)

System:

   Android SDK Tools : 26.1.1 (C:\Users\UsuarioDT01\AppData\Local\Android\Sdk)
   NodeJS            : v8.11.1 (C:\nodejs\node.exe)
   npm               : 5.6.0
   OS                : Windows 10

Also I don’t understand why the request is being sent as OPTION when it’s a GET in both server and client.

So requests to /videos-explicativos work now?
You can also call /videos-explicativos/foo for requests to https://myserver.net/videos-explicativos/foo.

Keyword you want to google for here is “CORS”.

Yes, with the proxy now it seems to work and the GET request doesn’t seem to be converting to an OPTION one.

It seems changing the proxy in as I said in my previous post worked

Thank you very much for your help and patience.