V
V
Vladimir2015-02-11 04:53:27
API
Vladimir, 2015-02-11 04:53:27

How to parse POST request with dynamic data in WEB API?

Good afternoon.
In my view, the script generates an unknown amount of data in advance, and then sends them with a post request. I found an article where it says that in the class you can make a Dictionary dictionary, and send data using []
So I did:

public Dictionary<string, object> Column { get; set; }

The script sends data like:
Column%5BP5%5D=&Column%5BP6%5D=71&Column%5BP7%5D=100%25+-+%D1%85%D0%BB%D0%BE%D0%BF%D0%BE%D0%BA&Column%5BP8 %5D=&Column%5BP9%5D=&Column%5BP10%5D=&

and then in the controller:
public async Task<ActionResult> EditAModel(ModelView data)
        {
                // обработка
        }

everything works well I 26c1db60db6e44b28b2250e9eb1d3f77.jpg
wanted to do the same but in ApiController. And the problems started. The code is the same, but data is no longer written to this dictionary... It's impossible to get through Request, because QueryString is always empty...
It turned out to get a string like this:
public async Task<IHttpActionResult> EditModelList()
        {
            string result = await Request.Content.ReadAsStringAsync();
            var data = JsonConvert.DeserializeObject<ModelView>(result); // Не парсит, выдает ошибку
            return Ok();
        }

But it fails to parse.
In a regular controller, everything went smoothly, but here ...
Maybe I didn’t finish reading something and it’s done differently in Api? How to be? What to do?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Pukhov, 2015-02-11
@Neuroware

What error does it give?

W
wkololo_4ever, 2015-02-11
@wkololo_4ever

So, you are not getting JSON, how are you going to parse it with JsonConvert?
Here result is a set of keys and values, not a json object.

S
sasha, 2015-02-11
@madmages

urlencoded string in post

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question