klautern
klautern

Reputation: 139

Request to swagger api with postman does not have body

I am making a request via postman to a swagger api, sending a body parameter. The problem is that in the api I cannot retrieve that parameter. Both req.body or req.swagger.params.body are undefined. In fact I've checked every property of the request object and the body is not there.

Is something wrong in my postman request or the swagger definition?

The request in postman:

Postman auto-genarated http code:

POST /myUrl/
Host: 127.0.0.1:10010
Content-Type: application/json
cache-control: no-cache
Postman-Token: xxxxx-xxxx-xxx-xxxx
{
    "username": "user2",
    "password": "pass2"
}------WebKitFormBoundary7MA4YWxkTrZu0gW--

Postman auto-generated curl code:

curl -X POST \
  http://127.0.0.1:10010/users/ \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: f8c71ed0-6ef1-4dcf-b1b6-a7f2777a759e' \
  -H 'cache-control: no-cache' \
  -d '{
    "username": "user2",
    "password": "pass2"
}'

The endpoint definition in swagger:

swagger: "2.0"
info:
  version: "0.0.1"
  title: XXXXX
host: 127.0.0.1:10010
basePath: /
schemes:
  - http
  - https
consumes:
  - application/json
produces:
  - application/json
tags:
  - name: xxx
    description: Endpoints callable by the xxx
paths:
  /users/:
    x-swagger-router-controller: user.controller
    post:
      tags:
        - xxx
      summary: Create a new user
      description: Create a new user
      operationId: createUser
      consumes:
        - application/json
      parameters:
        - in: body
          name: user
          description: The user to create
          schema:
            type: object
            required:
              - username
              - password
            properties:
              username:
                type: string
              password:
                type: string
      responses:
        200:
          description: Success
          schema:
            $ref: '#/definitions/CreateUserResponse'

enter image description here enter image description here

Upvotes: 1

Views: 2649

Answers (1)

Sam
Sam

Reputation: 6112

That ------WebKitFormBoundary7MA4YWxkTrZu0gW-- seems wrong.

Either your Content-Type is multipart/mixed; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW-- and use the boundary to separate form encoded parameters.

Or your Content-Type is application/json and you send json which doesn't require a boundary.

But as your swagger consumes application/json, make sure you remove the ------WebKitFormBoundary7MA4YWxkTrZu0gW-- from the body.

Upvotes: 1

Related Questions