Skip to content

Http: naked false/0 value is not correctly parsed when using json as response type #19825

@dzonatan

Description

@dzonatan

I'm submitting a...

[X] Bug report

Current behavior

http.get<boolean>(`/api/canYouSeeMe`)
  .subscribe(x => console.log(x));

In such case if backend returns false or 0 straight in the body (without any nested object) the console print outs null. While true or 1 keeps what it is.

Yes. I know that it's strongly recommended for json APIs to return pure values wrapped into some object, but still.. it's not forbidden and seems like javascript supports parsing such values.

And that's happening because of this line:

this.body = init.body || null;

Obviously:

this.body = true || null; // true
this.body = false || null; // null
this.body = 0 || null; // null

Expected behavior

Keep the falsy (false, 0) value unchanged.

Current workaround

Set the responseType to text and parse it for yourself.

Environment

Angular versions: 4.x, 5.0.0-rc.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions