Home > Back-end >  How can I test two strings I received with frombody in postman?
How can I test two strings I received with frombody in postman?

Time:08-30

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

CodePudding user response:

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": "[email protected]",
  "newPassword":"1234"
}

CodePudding user response:

You have to use correct JSON Format to use more than one field.

IMG JSON Object

{ "Email": "[email protected]", "NewPassword": "1234567890" }

Or

IMG JSON Array

[ "[email protected]", "1234567890" ]

If it's a JSON Object use {} or [] if it's JSON Array

  • Related