Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Web Api Multipost Parameter

So, i'm trying to pass multiple parameters from fiddler to my web api, using FormDataCollection.ReadAsNameValueCollection(). Problem is everytime is send my data, formData comes back as null. I'm not sure what I'm doing wrong here. I've tried decorating formData with a [FromBody] attribute. Also registered JsonMediaTypeFormatter() in the global.asax class.

Any help would be much appreciated.

Please see code below:

[HttpPost]
public HttpResponseMessage PostAccount([FromBody]FormDataCollection formData)
{
    if (formData != null)
    {
        var nValueCol = formData.ReadAsNameValueCollection();

        var account = new Account()
        {
            Email = nValueCol["email"],
            Password = nValueCol["password"],
            AgreedToTerms = Convert.ToBoolean(nValueCol["agreesToTerms"]),
            //LocationAccountCreated = DbGeography.FromText(nValueCol["userLocation"])
        };

        var userProfile = new UserProfile()
        {
            FirstName = nValueCol["firstName"],
            LastName = nValueCol["lastName"],
            DateOfBirth = Convert.ToDateTime(nValueCol["dateOfBirth"])
        };

        var newAcc = _accountService.CreateAccount(account.Email, userProfile.FirstName, userProfile.LastName,
                                                    userProfile.DateOfBirth, account.Email, account.AgreedToTerms,
                                                    account.LocationAccountCreated);
        var response = Request.CreateResponse(HttpStatusCode.Created);

        return response;
    }
    else
        return Request.CreateResponse(HttpStatusCode.NotAcceptable);
}

Sample request:

Fiddler Post Request

like image 493
user6201138 Avatar asked Dec 07 '25 07:12

user6201138


1 Answers

FormDataCollection is normally associated with application/x-www-form-urlencoded media type.

Your screen shot shows you are trying to send json data. If you don't have a concrete data type for your data and you want to send it as Json you can use an IDictionary<string,string> which will be mapped by the model binder successfully.

You action will look something like...

[HttpPost]
public HttpResponseMessage PostAccount([FromBody]IDictionary<string, string> formData) {
    if (formData != null) {
        var nValueCol = formData;
        //...other code removed for brevity, but can basically stay the same
        var response = Request.CreateResponse(HttpStatusCode.Created);
        return response;
    } else
        return Request.CreateResponse(HttpStatusCode.NotAcceptable);
}

Based on your code and the information from your fiddler screen shot, a TestController was created, a request was tested with fiddler like...

POST http://localhost:35979/api/account/create HTTP/1.1
User-Agent: Fiddler
Host: localhost:35979
Content-Type: application/json
Content-Length: 76

{"email":"[email protected]",
"firstname":"myFName",
"lastName":"myLName"}

...and the formData was populate with the 3 fields and their data.

like image 200
Nkosi Avatar answered Dec 08 '25 20:12

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!