Search code examples
angulartypescriptangular-httpangular-observable

net::ERR_EMPTY_RESPONSE error on DELETE Method in Angular 4?


I am using below method to delete data

return this.http.delete(this.ApiURL, new RequestOptions({headers: headers,body: body }))
.map((res: Response) => res.json());

But got net:: ERR_EMPTY_RESPONSE Also when I run this in POSTMAN it is successfully deleted.

Please guide


Solution

  • In your case the server returns an empty HTTP response body and status 200 OK. However, HttpResponse's .json() method expects the body to be a valid JSON string. Since you don't have a body, the .json() method throws a Unexpected end of JSON input error. You also don't have a .catch() and this unhandled error propagates to the browser console.

    One way to solve this is if you have access to the server side code. You can change your DELETE method's success response status code to be 204 ("No content"). In this way the .json() method will not throw an error, since it expects the response body to be empty.

    The other way is to take for granted that if the status code is 200, the deletion was completed successfully, and not to call .json() on the response object.