skip to Main Content

I made a project with swagger only for testing swagger and I get a 400 bad When I try to post what is wrong??

I do not have any problem geting the data from the array.

{
    "message": "Validation errors",
    "errors": [
        {
            "code": "INVALID_REQUEST_PARAMETER",
            "errors": [
                {
                    "code": "REQUIRED",
                    "message": "Value is required but was not provided",
                    "path": [
                        "paths",
                        "/recipe",
                        "post",
                        "parameters",
                        "0"
                    ]
                }
            ],
            "in": "body",
            "message": "Invalid parameter (body): Value is required but was not provided",
            "name": "body",
            "path": [
                "paths",
                "/recipe",
                "post",
                "parameters",
                "0"
            ]
        }
    ]
}

recipe.js file:

module.exports.createRecipe = (req, res) => {
  let newRecipe = req.body;
  newRecipe.id = recipeList.length;
  newRecipe.isPublic = true;
  recipeList.push(newRecipe);

  return res.status(204).end();
};
paths:
  /recipe:
    x-swagger-router-controller: repice
    get:
      description: Return all the recipes
      operationId: getAllRecipes
      responses:
        200:
          description: Success get all the recipes
          schema:
            type: array
            items:
              $ref: "#/definitions/Recipe"
        500:
          description: Unexpected Error
          schema:
            type: object
            properties:
              messeage:
                type: string
    post:
      description: Create one new Recipe
      operationId: postRecipes
      parameters:
        - in: body
          name: body
          description: The recipe to be added
          required: true
          schema:
            $ref: "#/definitions/Recipe"

      responses:
        204:
          description: Success adding the rescipe
        500:
          description: Unexpected Error
          schema:
            type: object
            properties:
              messeage:
                type: string

What Do I need to do to make it work???

What do I need to do to make

I expect a 204 request and the recipe will be added to the array

2

Answers


  1. Chosen as BEST ANSWER

    To make the body to work you need to add app.use(bodyParser.json()); in the app.js file

    var SwaggerExpress = require("swagger-express-mw");
    var express = require("express");
    
    var bodyParser = require("body-parser");
    
    var app = express();
    
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());
    

  2. Your swagger POST error 400 is a classic case of missing a required field in your request body. The message Value is required but was not provided is your clue here. Check your swagger definition for the /recipe POST operation and ensure all required fields are included in your request payload. This often happens when testing endpoints that expect specific data structures. Double-check your request against the swagger docs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search