3

I'm trying to pass some json as string in an action but it only works for the first parameter.

Here is the action :

[HttpPost("testy")]
public string testyJson([FromBody] String json, [FromBody] String shortJson)
{
    dynamic o = MyModule.replaceInJson(json, shortJson);

    return "";
}

Here is the json I pass to both params :

"{\"id\":1}"

Here is the result enter image description here

More informations :

I'm using swagger to test my api, here is what I entered enter image description here

Nkosi
  • 235,767
  • 35
  • 427
  • 472
galiolio
  • 275
  • 3
  • 19

2 Answers2

1

According to documentation

Binding formatted data from the request body

There can be at most one parameter per action decorated with [FromBody]. The ASP.NET Core MVC run-time delegates the responsibility of reading the request stream to the formatter. Once the request stream is read for a parameter, it's generally not possible to read the request stream again for binding other [FromBody] parameters.

(emphasis mine)

So the first parameter will get populated from the body but the second wont as the stream has already been read.

I would suggest creating a model to hold all the information to be passed to the action...

public class JsonModel {
    public string json { get; set;}
    public string shortJson { get; set;}
}

...and use that for the action parameter

[HttpPost("testy")]
public IActionResult testyJson([FromBody]JsonModel model) {
    var json = model.json;
    var shortJson = model.shortJson;
    dynamic o = MyModule.replaceInJson(json, shortJson);

    return Ok();
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

Answer found : It's blocks by the framework for performances reasons. So i'll use a list of string containing my jsons.

galiolio
  • 275
  • 3
  • 19