0

In Swagger is it possible to have the body represented as HTML forms as opposed to a JSON string? and how would one achieve this?

In django for example I can input the JSON via html forms (Yes this is a simple example, but more complex objects can be a little annoying to write JSON, especially where the field is a foreign key and you can't remember the entries in that table). enter image description here

In Swagger my UI looks like, ideally Id would also not be a required input as it is auto-generated. enter image description here

From the request body drop down box I only have these choices.
enter image description here

Any help much appreciated.

Soggy
  • 142
  • 1
  • 15
  • it would help if you showed your controller. If you want your controller endpoint parameters to be form fields, then you should use the `[FromForm]` parameter attribute. For example: `public IActionResult MyFormEndpoint([FromForm] MyFormObject formObject){...}` – Andy Jan 17 '21 at 09:01
  • Thanks Andy I've just got the auto built controllers, from memory they did not include this annotation, I'll give it a shot – Soggy Jan 18 '21 at 00:14
  • Hey @Andy, do you know how to exclude attributes from swagger forms, i.e. if I have and type which has a foreign key "ProjectID" and a property Project which is used only by the GET request, to allow me to pass back the Project object, how would I exclude Project from being required in the POST request? – Soggy Jan 20 '21 at 05:36
  • It looks complicated: https://stackoverflow.com/questions/41005730/how-to-configure-swashbuckle-to-ignore-property-on-model I would consider creating a DTO model instead and map it to the actual model. – Andy Jan 20 '21 at 06:25

1 Answers1

1

As Andy correctly noted in the comments the answer is to add the [FromForm] annotation to the argument in the endpoint function

enter image description here

Soggy
  • 142
  • 1
  • 15