banner image 1 banner image 2

POST API with request body as JSON in Swagger

March 13, 2023
3 mins
command
blog-img 1
Kripa K
Author

This article mainly discusses on “message body does not match the declared format” issue encountered while integrating Swagger for POST requests with a Ruby on Rails repository and how to resolve it.

By Kripa K — “An Inquisitive learner”


POST API with request body as JSON in Swagger
Swagger

The most recent versions of Ruby and Rails are being used to set up a Ruby on Rails repository. Swagger has been incorporated as an API documentation testing tool for the Rest APIs. Version details of ruby, rails, and swagger are provided below.

Ruby Version — 3.2.0

Rails Version — 7.0.4.2

Gems Used:

grape-swagger

grape-swagger-entity

Swagger UI Package: Version 4.15.5

Download the latest swagger UI package into your local machine and unzip it. To integrate swagger UI, copy the contents within the dist folder of Swagger UI into the public/api/docs folder of the rails repository.

To make swagger work, we have to add this one line to Api::V1::Base module

https://gist.github.com/kripz97/9f6cba36ed39e27767168b767a2a3975

Restart server and check http://localhost:3000/api/docs/index.html. Swagger should be enabled.

POST Requests in Swagger:

The request Content-Type was always set to application/json in the new Swagger UI, which was perfect for GET requests with parameters. It is different when it comes to POST requests. Though Content-Type is set as application/json, parameter content type is always set as form data.

Form Parameters:

Form parameters are used to describe the payload of requests whose content type is application/x-www-form-urlencoded (used to POST primitive values and arrays of primitive values) or multipart/form-data (used to upload files or a combination of files and primitive data). But in the new swagger request content type is always set as “application/json”

POST Request with form parameters:

Response with form parameters:

Even though form data is sent as param type, content-type is set as “application/json” which creates a conflict in the request header and parameter content type and throws an error message to send valid application/json in the request’s body.

Body Parameters(JSON or XML data):

We are aware that swagger’s default content type setting is application/json. Hence the request body for POST requests must be sent as JSON or XML data. The request body is referred to as body parameters in swagger. So param type should be set as body within swagger documentation for each parameter.

https://gist.github.com/kripz97/5695af3619022b8631782a5c5a0f56de
Body Parameters(JSON or XML data)

API Request:

Here the parameter content type is now set as application/json and parameters are added in JSON format. Now the request is executed successfully.

Response:

Thus the header content type and parameter type conflict are now resolved and we can test the POST API requests with swagger toolkit.


References:

[embed]https://swagger.io/docs/specification/2-0/describing-parameters/[/embed][embed]https://swagger.io/docs/specification/2-0/describing-parameters/[/embed]

Meet the team!

Author

Kripa K

Reviewed by

Harinniy Gopikannan

Editor

Seema Jain


We at CaratLane are solving some of the most intriguing challenges to make our mark in the relatively uncharted omnichannel jewellery industry. If you are interested in tackling such obstacles, feel free to drop your updated resume/CV to careers@caratlane.com
blog-img 2

Discussions

blog-img 3
5 mins
May 17, 2023
Sharing Data Between Controllers: Best Practices S...

This article will help you to understand the diffe

By Naveen C

blog-img 3
5 mins
March 21, 2023
Understanding Auto Layout and Constraints in Swift...

This article gives you an easy way of understandin

By Ramasamy P