0

I'm trying to post a json and handle the response but all I get is these errors:

zone-evergreen.js:2952 OPTIONS http://'api':10050/api/content/Delivery net::ERR_EMPTY_RESPONSE

core.js:6014 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://'api':10050/api/content/Delivery", ok: false, …}

I saw a lot of cases on stackoverflow and tried everything but nothing worked, when i try passing it through postman it works perfectly.

service:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable()
export class HttpService {

  private headers: HttpHeaders;
  private accessPointUrl: string = 'http://'api':10050/api/content/Delivery';


  constructor(private http: HttpClient) {
    this.headers = new HttpHeaders({'Content-Type': 'application/json; charset=utf-8'});
  }

  public get(payload){
    return this.http.get(this.accessPointUrl+ payload, {headers: this.headers});
  }


  public add(payload) {
    console.log(payload)
    return this.http.post(this.accessPointUrl, payload, {headers: this.headers});
}}

ts:

onSubmit(){

let json= JSON.stringify({
Name: "StackoverFlow"
})

 this.http.add(json).subscribe((data:any)=>{

console.log(data)
})

I tried everything and I'm really stuck, thank you in advance.

AliAb
  • 539
  • 3
  • 8
  • 31
  • 1
    Are you sure your URL is correct? `ERR_EMPTY_RESPONSE` means no response is coming from the URL. And also `http://'api':10050/api/content/Delivery` does not seem to be a valid url. – Harun Yilmaz Nov 29 '19 at 14:10
  • the 'api' means that I'm using a real api and I'm sure it's correct – AliAb Nov 29 '19 at 14:12
  • @R.Richards I'm using the add method – AliAb Nov 29 '19 at 14:15
  • It seems it is a timeout issue. Check whether the backend service is up. – hbamithkumara Nov 29 '19 at 14:45
  • This question looks similar, https://stackoverflow.com/questions/19858251/http-status-code-0-error-domain-nsurlerrordomain – AGoranov Nov 29 '19 at 14:46
  • Possible duplicate of [HTTP status code 0 - Error Domain=NSURLErrorDomain?](https://stackoverflow.com/questions/19858251/http-status-code-0-error-domain-nsurlerrordomain) – AGoranov Nov 29 '19 at 14:47

1 Answers1

1

You can leave out the headers because JSON is the default:

return this.http.post(this.accessPointUrl, payload);

Leave out the JSON.stringify it's handled by the HttpClient and probably causes your error:

this.http.add(json).subscribe((data:any)=>({
   name: "StackoverFlow"
}));
Marcel Hoekstra
  • 1,334
  • 12
  • 19
  • Not entirely right on leaving out the headers. Some services like express.js look for the header "application/json" in order to parse the body correctly on the server-side. The JSON.stringify() itself is perfectly fine and would not be the source of the OP's problem. – lionbigcat Nov 29 '19 at 14:53