简体   繁体   中英

How to assign headers and use the request body in angular 4 PUT request?

I am trying to upload a csv file using the HttpClient of angular 4 using the PUT request. The headers accepted is application/octet-stream.

My http put request is as below :

public uploadFileToDm(uploadUrl,file): any {
    return this.http.put(uploadUrl,file,{ headers: new HttpHeaders({ 'Content-Type': 'application/octet-stream' }) }).
    map((response: Response) => {
      if (response.status === 200) {
        console.log("confirm");
      } else {
        return Observable.throw(uploadUrl + 'upload failed:' + response.status);
      }
    }).catch((error: Response) => {
      console.log('Error occured while getting upload urls.' + error);
      return Observable.throw(error);
    });
  }

How can i get the response code and status?

The place where you subscribe to the event. move the code inside map operator to subscribe

this.uploadFileToDm().subscribe(data => {
  if (response.status === 200) {
        console.log("confirm");
      } else {
        return Observable.throw(uploadUrl + 'upload failed:' + response.status);
      }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM