0

I am creating a REST API with PHP CodeIgniter paired with an MYSQL Database and an AngularJS Frontend. This is how my Database Table looks like: https://i.sstatic.net/piMZS.png

What Works

  • I tested this API with a POST request from POSTMan like and it works perfectly.
account_name : "Test User 2"
display_name : "Test 2"
phone_no : 2832532
email : [email protected]
  • This is the PHP response
Array
(
    [account_name] => Test User 2
    [display_name] => Test 2
    [phone_no] => 2832532
    [email] => [email protected]
)

What Doesn't work

  • I am sending a request from Angular using HTTP Client. The Code:
  onSubmit(){
    let headers = new HttpHeaders();
    headers.append("Content Type", "application/json");
    headers.append("Accept", 'application/json');
    let postData = {
      account_name : "Test User 2",
      display_name : "Test 2",
      phone_no : "2832532",
      email : "[email protected]"
    }

    this.http.post("http://localhost:8080/api/user-info/create-user", JSON.stringify(postData), {headers: headers})
    .subscribe(data => {
      console.log(data);
     }, error => {
      console.log(error);
      console.log(JSON.stringify(postData))
    })
  }
}
  • The issue is after JSON.stringify(postData) the postData becomes
{"account_name":"Test User 2",
"display_name":"Test 2",
"phone_no":"2832532",
"email":"[email protected]"}
  • And I get an Internal Server Error 500
code: 500
message: "Column 'account_name' cannot be null"
title: "mysqli_sql_exception"
  • And the PHP Response is
Array
(
    [account_name] => 
    [display_name] => 
    [phone_no] => 
    [email] => 
)
  • The data is not receiving PHP whenever account_name becomes "account_name"

Any help with this will be highly appreciated. Thanks

1 Answer 1

1

From angular you POST the data as JSON. My guess is that with POSTMAN you send the data as form-data.

You could try the following:

onSubmit(){
  var formData: any = new FormData();
  formData.append("account_name", "Test User 2");
  formData.append("display_name", "Test 2");
  formData.append("phone_no", "2832532");
  formData.append("email", "[email protected]");

  this.http.post("http://localhost:8080/api/user-info/create-user", formData)
  .subscribe(data => {
    console.log(data);
   }, error => {
    console.log(error);
    console.log(formData)
  })
}

Or change your PHP code to accept JSON.

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.