Showing posts with label request body. Show all posts
Showing posts with label request body. Show all posts

Thursday, 19 March 2020

Swagger: Build request body

Using ‘requestBody’ element, you can build request body in swagger editor.

Example
  /employees:
      post:
        description: Add new employee to the organization
        requestBody:
          content:
            application/json:
              schema:
                type: object
                properties:
                  firstName:
                    type: string
                    example: ram
                  lastName:
                    type: string
                    example: gurram
        responses:
          201:
            description: New Employee is created
            content:
              application/json:
                schema:
                  type: object
                  properties:
                    firstName:
                      type: string
                      example: ram
                    lastName:
                      type: string
                      example: gurram
                    id:
                      type: integer
                      example: 1234

data.yaml
openapi: 3.0.0
info:
  title: Customer Data Aceess API
  description: API to expose all the CRUD operations on  customers
  contact:
    name: Krishna
    email: krishna123@abc.com
    url: https://self-learning-java-tutorial.blogspot.com/
  version: 1.0.0
paths:
  /employees/{employeeId}:
    get:
      parameters: 
      - in: path
        name: employeeId
        required: true
        schema:
          type: integer
          example: 123
      responses:
        200:
          description: Get Specific Employee Details
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: integer
                    example: 123
                  name:
                    type: string
                    example: krishna
  /employees:
      post:
        description: Add new employee to the organization
        requestBody:
          content:
            application/json:
              schema:
                type: object
                properties:
                  firstName:
                    type: string
                    example: ram
                  lastName:
                    type: string
                    example: gurram
        responses:
          201:
            description: New Employee is created
            content:
              application/json:
                schema:
                  type: object
                  properties:
                    firstName:
                      type: string
                      example: ram
                    lastName:
                      type: string
                      example: gurram
                    id:
                      type: integer
                      example: 1234
                  
      get:
        parameters: 
          - in: query
            name: from
            description: Page number to return
            required: true
            schema:
              type: integer
              example: 1
          - in: query
            name: size
            description: Number of elements to return
            required: false
            schema:
              type: integer
              example: 10
              minimum: 10
              maximum: 100
          - in: header
            name: onetime_token
            description: token to be used at the time of login
            required: false
            schema:
              type: string
              example: 08c3372a-9314-49d6-b9dd-ff212c1715a5
        responses:
          200:
            description: List of all the employees in organization
            content:
               application/json:
                  schema:
                    type: array
                    items:
                      properties:
                        id:
                          type: integer
                          example: 1234
                        name:
                          type: string
                          example: Krishna

Add the content of data.yaml to swagger editor, you can see the API definition in right side of the window.



Previous                                                    Next                                                    Home

Tuesday, 5 March 2019

express: req.body: Get the request payload


‘req.body’ is used to get the request payload.

Let’s see it with an example.

index.js
const express = require('express')

const app = express()

const port = 3000

app.get('/', (req, res) => res.send("Hello World"))

app.post('/users', (req,res) => {
 console.log(`Received : ${req.body}`)
 res.status(201).send(`Received : ${req.body}`)
})

app.listen(port, () => console.log(`Application started listening on port ${port}!`));

Run index.js

Open postman and fire below post request.

Method: POST
Body:
{
  "name" : "Krishna",
  "id" : "I12345"
}


Set the content-type header to application/json.


When I send the POST request, I got response as undefined.



Why req.body is undefined?
By default, req.body is undefined, and is populated when you use body-parsing middleware such as body-parser and multer.

How to use the body-parser?
Import body-parser api.
const bodyParser = require('body-parser');

Register the body-parser middlewares with express application.
// for parsing application/json
app.use(bodyParser.json());

// for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

index.js

const express = require('express')
const bodyParser = require('body-parser');

const app = express()

// for parsing application/json
app.use(bodyParser.json()); 

// for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true })); 

app.get('/', (req, res) => res.send("Hello World"))

app.post('/users', (req,res) => {
 var receivedData = JSON.stringify(req.body, null, 0)
 console.log(`Received: ${receivedData}`)
 res.send(receivedData)
})

const port = 3000
app.listen(port, () => console.log(`Application started listening on port ${port}!`));

Run index.js

Hit the same POST request again.

You can see below message in console and postman.

Received: {"{\r\n  \"name\" : \"Krishna\",\r\n  \"id\" : \"I12345\"\r\n}\r\n\r\n":""}



Previous                                                 Next                                                 Home