0

I am trying to call the http post call in angular with a body but I am not getting the response.

callAddGroupAPI(formId, groupJSON){
   let json = {
       "group":groupJSON
   }

    this.http.post(this.apiURL+'AddGroup/'+formId+'/3',{body:json}).subscribe(message => {
      alert("success");
      },
      (err) => {
        console.log(err)
      }
    )
 }

where 'this.apiURL/AddGroup/formId/groupId' is my URL and I want to send some JSON body where 'key = group' and "value=someJSON"

I am attaching the postman screenshot below

3 Answers 3

1

you need to add HttpHeaders in your post request like https://angular.io/guide/http#making-a-post-request

import { HttpHeaders } from '@angular/common/http';

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json',
    'Authorization': 'my-auth-token'
  })
};

addHero (hero: Hero): Observable<Hero> {
  return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
    .pipe(
      catchError(this.handleError('addHero', hero))
    );
}

Sign up to request clarification or add additional context in comments.

Comments

1

You can try this way:

 create(request: RequestData) {
 return this.http.post(this.shopService.getShopBaseUrl() + 
 'Requests', request, {
 headers: new HttpHeaders()
 .set('Content-Type', 'application/json')});

Comments

1

I think your problem its about body format. enter image description here

In postman your body its form-data, and in Angular if you don't define http body as form-data, for default it's a raw body, with a JSON object.

I think to revolve your problem you need to change your code to:

const body = new FormData();
body.set('group',groupJSON);
//or
body.append('group',groupJSON);

this.http.post(this.apiURL+'AddGroup/'+formId+'/3',body).subscribe(message => {
  alert("success");
  },
  (err) => {
    console.log(err)
  }
)

2 Comments

so, Is that x.set() or body.set ??
body.set() sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.