Sajjad
Sajjad

Reputation: 893

Argument of type 'HttpRequest<FormData>' is not assignable to parameter of type 'string | Request'

I am trying to post a file with http client in this way

const req = new HttpRequest('POST', this.API, formData, { reportProgress: true });

        this.http.request(req).subscribe(event => {
            if (event.type === HttpEventType.UploadProgress) {...}

But typescript shows me the warning

Argument of type 'HttpRequest<FormData>' is not assignable to parameter of type 'string | Request'.
Type 'HttpRequest<FormData>' is not assignable to type 'Request'.
    Property 'contentType' is missing in type 'HttpRequest<FormData>'.

Upon executing I see following error in console

Error: First argument must be a url string or Request instance.

while according to angular's http.request definition the first argument can be either a string or of type Request

(method) Http.request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>

Also I didn't understand about the property contentType missing warning, does that mean I have to explicitly include it in header?

Is there something I doing wrong here?

Edit: I am using

"@angular/common": "6.0.0",
"@angular/http": "6.0.0",

Upvotes: 1

Views: 4041

Answers (1)

SiddAjmera
SiddAjmera

Reputation: 39432

From the error message, it seems that you're using Http instead of HttpClient. Try changing from Http to HttpClient and check if that works for you.

Don't forget to add the HttpClientModule to the imports array of your @NgModule.

Http's request method used to accept the request of type string | Request. HttpClient's request method on the other hand accepts a request of type HttpRequest

Upvotes: 2

Related Questions