1

hi i need some help regarding posting to .net core web api. i am new to angular.

im trying to post a request at https://localhost:44394/api/SyUsers but i get 415 error i tried to set const headers = new HttpHeaders(); headers.set('Content-Type', 'application/json');

here is the post method:

  addPost(
    authType: string,    
    userId: string,
    userCreateDate: any,
    username: string,
    firstName: string,
    middleName: string,
    lastName: string,
    email: string,
    position: string,
    section: string,
    department: string,
    alias01: string,
    alias02: string,
    alias03: string,
    alias04: string,
    userModifiedDate: any
    ) {
    const userData = new FormData();
    userData.append('authType', authType);
    userData.append('userId', userId);
    userData.append('userCreateDate', userCreateDate);
    userData.append('username', username);
    userData.append('Hash', localStorage.getItem('userId').toString());
    userData.append('firstName', firstName);
    userData.append('middleName', middleName);
    userData.append('lastName', lastName);
    userData.append('email', email);
    userData.append('position', position);
    userData.append('section', section);
    userData.append('department', department);
    userData.append('alias01', alias01);
    userData.append('alias02', alias02);
    userData.append('alias03', alias03);
    userData.append('alias04', alias04);
    userData.append('userPermissions', 'sa');
    userData.append('userGroup', 'sa');
    userData.append('userModifiedDate', userModifiedDate);
    userData.append('userModifiedBy', localStorage.getItem('username').toString());
    userData.append('userCreatedBy', localStorage.getItem('username'));
    
    this.http
    .post<{message: string, user: Users}>(
      BACKEND_URL, 
      JSON.stringify(userData),
      {headers: headers}
    )
    .subscribe((responseData) => {
      this.router.navigate(['user-list']);
    });
    console.log(userData)
  }

and this is the code from .net core

[HttpPost]
public async Task<ActionResult<SyUsers>> PostSyUsers([FromBody]  SyUsers userData)
{
    _context.SyUsers.Add(userData);
    await _context.SaveChangesAsync();

    return CreatedAtAction("GetSyUsers", new { id = userData.Id }, userData);
}

on postman it is working:

postman works

2
  • Can you share your postman request body? Commented Jan 23, 2021 at 9:09
  • Use a plain javascript object instead of "FormData()": const userData = { authType: authType ... } Commented Jan 23, 2021 at 12:39

1 Answer 1

1

In postman, you encapsulate the data in the body, and the bakend is [FromBody], so it can run correctly. But in angular, you use FormData, the receiving type is different.

Change [FromBody] to [FromForm], and remove the JSON.stringify in angular.

this.http
.post<{message: string, user: Users}>(
  BACKEND_URL, 

  userData,
  {headers: headers}
)
.subscribe((responseData) => {
  this.router.navigate(['user-list']);
});

enter image description here

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

2 Comments

@Christian Lacuesta, could you accept this answer?
yes thanks so much karney it was great help.

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.