capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

bug: Capacitor http request failed on native platforms when response body is null (js null value) and Content-Type is application/json

Open dimitr0v opened this issue 9 months ago • 0 comments

Bug Report

Capacitor Version

Installed Dependencies:

  @capacitor/core: 5.4.1
  @capacitor/cli: 5.4.1
  @capacitor/android: 5.4.1
  @capacitor/ios: 5.4.1

Platform(s)

Android and iOS

Current Behavior

When XHR-Patching is enabled on both Android and iOS platforms and the response body of a GET request is null (js null value) and the Content-Type is "application/json", an error is thrown in the native-bridge code which causes the request to fail in the application code.

http-null-response-bug

Expected Behavior

The request should complete successfully and the response body should be null, as returned from the native code. When the platform is web it behave in exactly that way.

Code Reproduction

Using Angular v16.1.6 as a front-end framework.

import { HttpClient } from '@angular/common/http';
// ...
constructor(private http: HttpClient){}
// ...
this.http.get('http://domain.com/api/endpoint').subscribe(res => { // <-- "res" should be null
        console.log(res); // <-- never called
      }, error => {
        console.error(error); // <-- but error is thrown
      });

Other Technical Details

npm --version output: 9.5.1

node --version output: v18.16.0

Additional Context

The error occurs in the native-bridge.ts on that line when trying to call .length on nativeResponse.data and its value is null.

dimitr0v avatar Sep 29 '23 12:09 dimitr0v