1

I have a controller endpoint as shown here which accepts an ExamCreateDTO from the form.

public async Task<ActionResult<IResponseData<uint>>> Create([FromForm] ExamCreateDTO examCreateDto)

These are the simplified ExamCreateDTO and ReservationQuota classes:

public class ExamCreateDTO : NamedObjectDTO
{
    public Scope Scope { get; set; }
    public Mode Mode { get; set; }
    public List<ReservationQuota> ReservationQuota { get; set; }
    public IEnumerable<IFormFile>? SampleFormFiles { get; set; }
}

public class ReservationQuota
{
    public string Category { get; set; }
    public short Reservation { get; set; }
}

I am trying to send the request by filling the form in Swagger. I am getting all fields correctly including form files in ExamCreateDTO, except ReservationQuota.

ReservationQuota count is 0, even though I am sending data in Swagger.

When I inspect HttpContext.Request.Form keys and store data for ReservationQuota is proper like this:

{
  [ReservationQuota, { { "category": "General", "reservation": 50 } } ] 
}

Stuck with this for quite some time. Any help/hint is greatly appreciated.

15
  • Ok ... let´s try to trouble shoot this. Can you temporarily set ReservationQuota to a list containing a simpler type - for instance a string ? ` public List<string> ReservationQuota { get; set; } ` Then build a query from there and see what happens. Commented Dec 27, 2023 at 6:22
  • 1
    Swagger gives a form UI where i can add the ReservationQuota objects to a list. I have no control of how I add the objects in swagger. I tried sending the value you suggested [{ "category": "General", "reservation": 50 }, { "category": "Special", "reservation": 25 } ] for ReservationQuota in Postman and still the ReservationQuota count is 0. The HttpContext.Request.Form keys and store data in this case is : {[ReservationQuota, {[{ "category": "General", "reservation": 50 }, { "category": "Special", "reservation": 25 } ]}]} Commented Dec 27, 2023 at 7:43
  • 1
    When I send the ReservationQuota as below in Postman UI as request body form data, it works. ReservationQuota[0].Category General ReservationQuota[0].Reservation 55 ReservationQuota[0].Category Special ReservationQuota[0].Reservation 45 I will check the difference in the raw request formed in the working and non working case and update here shortly. Commented Dec 27, 2023 at 7:48
  • 1
    @BrianAndersen I have placed a simple project at git location: github.com/manjunath-vadigeri/TestApp There is an endpoint as below which accepts ExamCreateDto public void Create([FromForm] ExamCreateDTO dto) Commented Dec 27, 2023 at 9:35
  • 1
    @BrianAndersen Thanks for your support. I will try with the approach mentioned in the above link. Commented Dec 28, 2023 at 10:59

1 Answer 1

1

Actually, this is known issue for the swagger UI and not resolved yet now.

The reason why the swagger send request doesn't match is it render the request as the json format but it should be multipart/form-data, since you set the fromform inside the controller method.

Like below:

enter image description here

So for this issue, we have two workaround now, one is creating a custom model binder to let the api to work with this specific request(please notice: after using this the right format for multiple form data will not work), another is using Frombody instead of fromform.

More details, you could refer to this answer.

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

2 Comments

I am facing problems sending the List via Postman as well. I tried sending the array [{ "category": "General", "reservation": 50 }, { "category": "Special", "reservation": 25 } ] for ReservationQuota in Postman and still the ReservationQuota count is 0. The HttpContext.Request.Form keys and store data in this case is : {[ReservationQuota, {[{ "category": "General", "reservation": 50 }, { "category": "Special", "reservation": 25 } ]}]}
If in the request the ReservationQuota object is flattened as below, it works ReservationQuota[0].Category: "General" ReservationQuota[0].Reservation: "50" ReservationQuota[1].Category: "Special" ReservationQuota[1].Reservation: "55" But does not work in case the request has json object associated with ReservationQuota as below ReservationQuota: "[{ "category": "General", "reservation": 50 }, { "category": "Special", "reservation": 25 } ]"

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.