0

I have .NET Core back end, that receive DTO as form-data.

Here is controller

 [HttpPost]
    public async Task<IActionResult> Register([FromForm] RegisterDto model)
    {
        var result = await _authAppService.Register(model);
        if (result.Code == 409)
        {
            return BadRequest();
        }

        return Ok(result.Token);
    }

Here is DTO

 public class RegisterDto
{
    [Required]
    public string Email { get; set; }
    [Required]
    public string Password { get; set; }
    [Required]
    public string PasswordConfirmation { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    public string GcmToken { get; set; }
}

I need to send data from Angular app via form-data

I defined model at Angular side

Here is it

export class RegisterDto{
Email: string;
Password: string;
PasswordConfirmation: string;
FirstName: string;
LastName: string;

}

And this is how I try to do form-data sending

register(){
  const formData =  new FormData();
  formData.append(this.registerObject);

}

in append method I have this erroк

Expected 2-3 arguments, but got 1.ts(2554)

How I can send DTO via form data?

1 Answer 1

1

You can't append the whole object without giving it a field name. As the error says it acceptes 2-3 arguments however you are only providing one argument without giving it a field name.

register(){
     const formData = new FormData();
     formData.append('regObj', this.registerObject);
}

however it's good if you append each field as formdata value such as.

formGroup: FormGroup;
formData: FormData;

register() { 
    this.formData.append('Email', this.formGroup.controls.Email.value);
    this.formData.append('Password', this.formGroup.controls.Password.value);
    this.formData.append('FirstName', this.formGroup.controls.FirstName.value);
    this.formData.append('LastName', this.formGroup.controls.LastName.value);

    // call your service and send it
}
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.