Search code examples
c#postmanhttp-post

How can I test two strings I received with frombody in postman?


I have an httppost method, this method performs the forgot password process and for this, it takes the user's mail and the newly entered password, I received them as FromBody, but while testing the postman I am getting the following error. How can I test these?


[HttpPost("forgetuserpassword")]
    public async Task<IActionResult> forgetUserPassword([FromBody]string user_mail,string new_password)
    {
        return Ok( await _userService.ForgetPassword(user_mail, new_password));
       
        
    }

I have another httppost method that I get with frombody but I only get one value in it and it works when I test postmande like this. However, when I do the same for the post method where I get these two strings, it gives an error.

This is how I can test the post method in the postman, where I get a single string value with frombody.

enter image description here

this is how i tried the code i shared above in postman.logically it should be like this

enter image description here


Solution

  • Your second post body is not valid JSON, try:

    [
      "string1",
      "string2"
    ]
    

    If you want to allow for multiple strings to be accepted, update your Method signature to this:

    public async Task<IActionResult> forgetUserPassword([FromBody]List<string> strings)
    

    But, I would recommend using an Object in this case, like so:

    public ForgotPasswordObject 
    { 
        public string Email {get;set;}
        public string NewPassword {get;set;}
    }
    

    Then your method could look like this:

    public async Task<IActionResult> forgetUserPassword([FromBody]ForgotPasswordObject forgotPassword)
    {
        return Ok( await _userService.ForgetPassword(forgotPassword.Email, forgotPassword.NewPassword));
    }
    

    And finally, your JSON would be structured like:

    {
      "email": "myemail@whatever.com",
      "newPassword":"1234"
    }