Angular 7 http.post JSON number sent as string

Viewed 1417

I have an Angular class defined with a property (timeout) of type 'number':

export class EngineSpec {
    name: string;
    base_url: string;
    version: string;
    timeout: number;
    tls: boolean = false;
    user_agent: string;
  }

However, when I http.post an instance of this class, the property is sent as a string:

{"tls":false,
  "name":"local-vm",
  "base_url":"http://localhost:2367",
  "version":"1.3",
  "timeout":"60"
}

Any ideas why a number is getting sent as a JSON string?

1 Answers

Turns out this is likely an issue with Material Angular. Previously I did not specify a 'type' on the input element. When I add 'type = number' (as below), the JSON represents timout as a number. I would think that since the underlying class represents timeout as a number, something along the process pipeline would have converted the input to a number. Anyway, specifying the type on the input element fixed my problem.

  <mat-form-field>
    <input matInput #input [readonly]="editing?null:''" [(ngModel)]="engineSpec.timeout" placeholder="Timeout" type="number"/>
    <mat-hint align="end">Enter timeout (in seconds) for API calls to this engine.</mat-hint>
  </mat-form-field>
Related