Hasitha
Hasitha

Reputation: 67

angular ionic http request sends null data to rest API

I'm trying to send data from an angular form to and endpoint. This angular form is in a ionic 4 app. But all I'm getting is null data at the back end. But when I try the same with postman it works fine.I also tried changing content type to "application/json" but it also gave the same result. For the back end I'm using an php api.

// Http Options,where I set http headers
  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'multipart/form-data;boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
  };


// Create a new user
  createUser(user: User): Observable<User> {
    return this.http
      .post<User>(this.url + 'test/create' , user , this.httpOptions)
      .pipe(
        retry(0),
         // handle error method is already implemented above     
        catchError(this.handleError)
      );
  }
}


I'm not receiving any errors in chrome console.

Upvotes: 0

Views: 285

Answers (1)

Patricio Vargas
Patricio Vargas

Reputation: 5522

You need to pass the data as a form data as follow:

const newUser = new FormData(); 
newUser.append('firstName', user.firstName);

Then in your post you pass newUser.

Upvotes: 1

Related Questions