2

With an endpoint like this:

        [HttpPost("[action]")]
        public async Task<ActionResult> Import([FromBody]string request)
        {
            var list = JsonConvert.DeserializeObject<List<RequestQuote>>(request);

            return NoContent();
        }

request always seems to be null. It worked for a little while earlier today but not sure what changed.

Here is the client side where I do the call.

  var json = JsonConvert.SerializeObject(payload);
  var data = new StringContent(json, Encoding.UTF8, "application/json");
  var response = client.PostAsJsonAsync($"{endpoint}/api/v1.0/BatchImport/Import", data);

Payload is a List

Even when using

Import([FromBody] List<RequestQuote> request)

I get the same issue.

dbc
  • 104,963
  • 20
  • 228
  • 340
Bike_dotnet
  • 234
  • 6
  • 18
  • 2
    What is `payload`? – Igor Dec 23 '21 at 20:13
  • 1
    Hook up something like Fiddler and inspect the HTTP request sent to the server, if it's malformed then that's your problem, if not then update the question with the request – MindSwipe Dec 23 '21 at 20:19
  • @MindSwipe, just tried with fiddler, looks like only the headers are there. But when I step through the code payload populates as it should – Bike_dotnet Dec 23 '21 at 20:46
  • What framework + version are you using? [tag:asp.net-core-webapi] or [tag:asp.net-web-api]? Might you please [edit] your question to [tag](https://stackoverflow.com/help/tagging) it with the appropriate framework? You're more likely to get help if you do. – dbc Dec 24 '21 at 14:45

3 Answers3

2

There is a bug in the code, the data is serialized twice, the second time when you use PostAsJsonAsync. Try this

var json = JsonConvert.SerializeObject(payload);

var data = new StringContent(json, Encoding.UTF8, "application/json");

var response = await client.PostAsync($"{endpoint}/api/v1.0/BatchImport/Import", data);

if (response.IsSuccessStatusCode)
    {
        var stringData = await response.Content.ReadAsStringAsync();
        var result = JsonConvert.DeserializeObject<object>(stringData);
    }

and action should be

public async Task<ActionResult> Import([FromBody] List<RequestQuote> request)
Serge
  • 40,935
  • 4
  • 18
  • 45
1

If your controller has the [ApiController] attribute, you can put the data type you want to parse as the parameter of the method. I believe what is happening is that your program is trying to parse the JSON to a string type, which isn't what you want. You can try the code below, which should achieve what you're looking for.

[HttpPost("[action]")]
public async Task<ActionResult> Import([FromBody]List<RequestQuote> list)
{
    // The parsed object should now be available here as "list"
    return NoContent();
}

Alternatively, this post suggests pulling the body of the request directly. Either solution should be valid.

0

json is coming properly for me. but after staticContent, I think it breaks down. The model I sent to Service is null. Here are my project codes : How can I send Model to service in xamarin forms. Something wrong while posting?