0

How do I get this web api request payload to be read as a normal json object inside the webapi. I'm sure I have to adjust the format somehow but I'm not familiar with how to read this and/or switch it to populate a custom object in the webapi.

here is a chrome dev screen to show how its coming over enter image description here

here are some fiddler screenshots enter image description here

enter image description here

my api... I'm trying different ways but bottom line is I need it to return a byte array since the going is to download a file.

here is the webapi

        [Route("api/excel/DownloadTopdayTrades2/")]
    [HttpPost]
    public HttpResponseMessage DownloadTopdayTrades2([FromBody]ListTradeDetailDTO trades)
    {
3
  • Can you show the code of the controller that is handling the request? Commented Feb 21, 2017 at 18:49
  • I added in my controller Commented Feb 21, 2017 at 18:51
  • I have edit my comment below, I think that you are missing the [FromBody] atrribute! Commented Feb 21, 2017 at 18:55

1 Answer 1

1

You have multiple solutions, you can read the body of the request and binding directly to a C# class var result = await Request.Content.ReadAsAsync<IEnumerable<ClassName>>(); or you can read it to a dynamic variable

dynamic obj = await Request.Content.ReadAsAsync<JObject>();
var y = obj.trades[0].AccountNumber;

EDIT: public byte[] DownloadTopdayTrades([FromBody]IEnumerable<TradeDetailDTO> trades) reads the content from the body request!

Sign up to request clarification or add additional context in comments.

12 Comments

I tried your edit but its null. as for dynamic variable example,, I get await must be task
The problem is that you have an object and inside that object you have the trades property! You have to create a new DTO that has a property trades of type List<TradeDetailDTO > and then the serialization will work!
public class MyDTO { public List<TradeDetailDTO> trades { get; set; } } public byte[] DownloadTopdayTrades([FromBody] MyDTO trades) You have to create a new DTO with property trades and then change the action signature!
Try to read the form date like this HttpResponseMessage DownloadTopdayTrades2(FormDataCollection formData)
No problem, dont forget to upvote my answers! :) var dto = JsonConvert.DeserializeObject< MyDTO >(json); try this way, I am assuming that you are using Newtonsoft Json Library
|

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.