0

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.
7
  • 1
    before your return, write a console.log(user); and let me know what you see Commented Nov 7, 2019 at 3:05
  • @devpato this is the output I get in console " User {firstName: "Hasitha"} " Commented Nov 7, 2019 at 4:38
  • Try passing the value as a form data. const newUser = new FormData(); newUser.append('firstName', user.firstName); then in your post you pass newUser. Commented Nov 7, 2019 at 4:47
  • Why do you need the pipe? Can't you send the error from the backend as status 500 for example? Commented Nov 7, 2019 at 7:36
  • @devpato Thank you. There was an issue in the way I was taking inputs Not passing the value as a form data was the issue. Tried your way and worked. Thank you again. Commented Nov 7, 2019 at 8:32

1 Answer 1

1

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.

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

Comments

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.