Home > Back-end >  Cannot convert data to json in angular 13 Content type 'text/plain;charset=UTF-8' not supp
Cannot convert data to json in angular 13 Content type 'text/plain;charset=UTF-8' not supp

Time:01-20

I am trying to submit a form in following way:

saveBuildcompany(): void {
    // @ts-ignore

  
    // @ts-ignore
    console.log(this.group?.value);
    
   let data2=this.group.value;
    let serializedForm = JSON.stringify(data2);

   console.log(data2);
   // data2.sociallinks.stringify;
    this.buildcompanyService.create(serializedForm)
      .subscribe({
        next: (res) => {
          console.log(res);
          this.submitted = true;
        },
        error: (e) => console.error(e)
      });
  }

The service is as follows:

create(data: any): Observable<any> {
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    headers.append('Accept', 'application/json');
    return this.http.post(baseUrl "/add", data, {headers: headers});
  }

After all I get the exception like in a title. What I am doing wrong?

CodePudding user response:

Change "Content-Type" to application/x-www-form-urlencoded and "Accept" application/json if you are sending form data.

create(data: any): Observable<any> {
    let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'});
    return this.http.post(baseUrl "/add", data, {headers: headers});
  }

or

create(data: any): Observable<any> {
    let headers = new HttpHeaders();
    headers.set('content-type', 'application/x-www-form-urlencoded')'
...
    return this.http.post(baseUrl "/add", data, {headers: headers});
  }

CodePudding user response:

You can Change "Content-Type" to application/x-www-form-urlencoded and "Accept" application/json. and try this below is the code snip.

create(data: any): Observable<any> {
    let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'});
    return this.http.post(baseUrl "/add", data, {headers: headers});
  }

CodePudding user response:

And just to conclude all I sad above. The actual create function is look like this. This is sending correct header information:

create(data: any): Observable<any> {
    let headers = new HttpHeaders({'Content-Type': 'application/json', 'Accept': 'application/json'});
    return this.http.post(baseUrl "/add", data, {headers: headers});
  }
  •  Tags:  
  • Related