I'm coding a website using Swagger 3.0/OpenAPI to describe my APIs, and Node.js for the back-end. I successfully made the GET requests work but I'm having some trouble with all the POST requests. The problem is that the parameters I provide from an HTML form while performing the POST request seem not to be received by the auto-generated by Swagger handlers in the /controllers folder. In particular, the req.swagger.params array and req.swagger.params['body'], sub-fields of the HTTP request object, that is supposed to contain the parameters, are always empty.
I tried to use both 'application/x-www-form-urlencoded' and 'application/json' as ContentType; I tried different sources to perform the request such as the SwaggerUI, Postman, the front-end web page and CURL; I tried both the FetchAPI and JQuery AJAX ($.post and $.ajax).
Here it's how I perform the POST request in Register.js:
event.preventDefault();
var firstname = $('#inputFirstname').val();
var lastname = $('#inputLastname').val();
var email = $('#inputEmail').val();
var psw = $('#inputPassword').val();
var data = {
'firstname': firstname,
'lastname': lastname,
'email': email,
'password': psw
};
$.ajax({
url: '/user/register',
type: 'POST',
dataType: 'text', // also tried json
contentType: 'application/x-www-form-urlencoded', //also tried json
data: data
}
)
.done(successScreen)
.fail(() => console.log('Fail'));
Here it's how I describe my API in swagger.yaml:
(...)
/user/register:
post:
tags:
- user
summary: Create a new user.
description: Register into the store.
operationId: userRegisterPOST
requestBody:
content:
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/RegisterBody'
responses:
201:
description: succesfull registration
content: {}
x-swagger-router-controller: User
(...)
RegisterBody:
required:
- email
- firstname
- lastname
- password
type: object
properties:
email:
type: string
password:
type: string
firstname:
type: string
lastname:
type: string
Here it's the auto-generated handler in /controllers/User.js for the POST requests on /user/register:
49:module.exports.userRegisterPOST = function userRegisterPOST (req, res){
50: var body = req.swagger.params['body'].value; // here it's where I get the error, but it's auto-generated code
51: User.userRegisterPOST(body)
52: .then(function (response) {
53: utils.writeJson(res, response);
54: })
55: .catch(function (response) {
56: utils.writeJson(res, response);
57: });
58:};
The error I get is:
TypeError: Cannot read property 'value' of undefined
at userRegisterPOST (/.../controllers/User.js:50:41)
at swaggerRouter (/.../node_modules/oas3-tools/middleware/swagger-router.js:388:20)
at Layer.handle [as handle_request] (/.../node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/.../node_modules/express/lib/router/index.js:317:13)
at /.../node_modules/express/lib/router/index.js:284:7
at Function.process_params (/.../node_modules/express/lib/router/index.js:335:12)
at next (/.../node_modules/express/lib/router/index.js:275:10)
at /.../node_modules/oas3-tools/middleware/swagger-validator.js:388:30
at /.../node_modules/async/dist/async.js:1140:9
at /.../node_modules/async/dist/async.js:473:16
at eachOfArrayLike (/.../node_modules/async/dist/async.js:1057:9)
at eachOf (/.../node_modules/async/dist/async.js:1117:5)
at _asyncMap (/.../node_modules/async/dist/async.js:1133:5)
at Object.map (/.../node_modules/async/dist/async.js:1122:16)
at swaggerValidator (/.../node_modules/oas3-tools/middleware/swagger-validator.js:358:15)
at Layer.handle [as handle_request] (/.../node_modules/express/lib/router/layer.js:95:5)
I'm supposed to find the params in req.swagger.params['body'].value
as for all the GET request.