Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Unable to bypass CORS in webview #249

Open
MeghaRajpara opened this issue Apr 6, 2022 · 1 comment
Open

Unable to bypass CORS in webview #249

MeghaRajpara opened this issue Apr 6, 2022 · 1 comment

Comments

@MeghaRajpara
Copy link

MeghaRajpara commented Apr 6, 2022

Describe the bug
Unable to bypass CORS in webview

Whenever I am trying to do POST request using capacitor-community/http getting CORS error. Doesn't get this error while making GET request.

Screen Shot 2022-04-05 at 9 08 42 PM

http.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Http, HttpOptions } from '@capacitor-community/http';
import { from, Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class HttpService {

  constructor() { }

  doGet(url) {
    const options: HttpOptions = {
      url
    }
    return from(Http.get(options));
  }

  doPost(url): Observable<any> {
    const options: HttpOptions = {
      url,
      method: 'POST',
      headers: { 'content-type': 'application/json','Authorization': 'Bearer id_token' },
      data: { foo: 'bar', cool: true },
    };

    return from(Http.request(options));
  }
}

home.page.ts

import { Component } from '@angular/core';
import { HttpService } from '../services/http.service';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  data = null;
  constructor(private httpService: HttpService) { }

  nativeCall() {
    this.httpService.doPost('https://xxxxx/cloude_function_name').subscribe((res: any) => {
      console.log(res);
      this.data = res.data.specials;
    });
  }

}
@maxhedt
Copy link

maxhedt commented Apr 8, 2022

You could try:

const options: HttpOptions = { url, webFetchExtra: { mode: 'no-cors' } }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants