0

Is there a trick to getting formdata from a post request in web api? I can't seem to be able to use namevaluecollection or formdatacollection when submitting a form on the frontend using js FormData(). I have checked the http request to verify the form data is being passed to the server.

JS Form Submit

    var formData = new FormData();
    formData.append('Email', '[email protected]');
    formData.append('Password', '123');
    // submit data using xmlhttprequest

Controller Web Api

public IHttpActionResult CheckUser(FormDataCollection FormData)
{
     //formdatacollection and namevaluecollection don't seem to return key based values
}

1 Answer 1

2

Actually, in web api controllers you should check the property Request to get all information you post from your page. Next you can get your data by using appropriate method: Request.Content.ReadAsByteArrayAsync or Request.Content.ReadAsFormDataAsync (it's probably your case) or Request.Content.ReadAsStreamAsync, etc. It depends on the sent data format.

Or you can always create a class with the respective fields and send it in JSON to get it another way:

public IHttpActionResult CheckUser(AccountData data)
{

}

And your class should look like:

public class AccountData {    
   public String Email { get; set; }    
   public String Password { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the input

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.