Noor Shaker
Noor Shaker

Reputation: 57

how to post reactive form data to REST API

I built angular reactive form on top of REST API and I need to submit (POST) the data to the API.

this is my POST function in the service class

postHotel(h :Hotel) {
    var body = JSON.stringify(h);
    var header = new header({'content-type' : 'application/json'});
    var request = new RequestOptions({method: RequestMethod.Post , headers:header});
    return this.http.post('http://localhost:56229/api/Hotels' ,body,request).map(x => x.json());
  }

and this is my form

<form  [formGroup]="profileForm" (ngSubmit)="onSubmit()">
    <div class="form-group">
      <div class="form-group col-md-6">
        <label >Hotel Name</label>
        <input class="form-control" type="text" formControlName="HotelName" required>
      </div> 
      <div class="form-group col-md-6">
          <label >Owner Name</label>
          <input class="form-control"  type="text" formControlName="OwnerName" required>
        </div>

    </div></form>

Upvotes: 0

Views: 7620

Answers (1)

Alex
Alex

Reputation: 1158

you can use the value property of your reactive form.

this.API.postHotel(this.profileForm.value);

Check out the reactive form documentation here: https://angular.io/guide/reactive-forms#saving-form-data

Upvotes: 2

Related Questions