1

I want to send the raw body from the swagger I' am using the below code: So based on the below code the API should get the raw body. Even though it's a post request the data is not being set in the raw body of the API request.

/**
 * @swagger
 * /api/v1/index/nocache?method=checkIfPaymentIsValidForDays:
 *    post:
 *     summary: Frontend API
 *     description: Check If Custom Payment Link Is Valid For Given Days
 *     consumes: 
 *       - application/json
 *     parameters:
 *       - in: data
 *         name: data
 *         description: The data will contain order_id and valid_for
 *         schema:      # Request body contents
 *           type: object
 *           properties:
 *              order_id: 
 *                type: string
 *              valid_for:
 *                type: integer
 *     responses:
 *       '200':
 *          description: Response success or failure
 *          content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                 status:
 *                   type: string
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: object
 *               example:
 *                  code: 200
 *                  status: success
 *                  msg: success
 *                  data: {"isValid": true}
 */

/*=================================*/

But on the api end I am getting empty raw body as shown below:

can't parse JSON.  Raw result:

<pre>
Var: Zend_Controller_Request_Http Object
(
    [_paramSources:protected] => Array
        (
            [0] => _GET
            [1] => _POST
        )

    [_requestUri:protected] => /apachedev/git/ice9/frontend/public/api/v1/index/nocache?method=checkIfPaymentIsValidForDays
    [_baseUrl:protected] => /apachedev/git/ice9/frontend/public
    [_basePath:protected] => 
    [_pathInfo:protected] => /api/v1/index/nocache
    [_params:protected] => Array
        (
            [module] => api
            [controller] => v1
            [action] => index
        )

    [_rawBody:protected] => 
    [_aliases:protected] => Array
        (
        )

    [_dispatched:protected] => 1
    [_module:protected] => api
    [_moduleKey:protected] => module
    [_controller:protected] => v1
    [_controllerKey:protected] => controller
    [_action:protected] => index
    [_actionKey:protected] => action
)

-File: /var/www/html/apachedev/git/ice9/frontend/application/modules/api/controllers/V1Controller.php -Line: 9763
</pre>

As you can see the _rawBody is coming empty.

Please provide any solution on this, I'm new to swagger.

Aditya Vyas
  • 21
  • 1
  • 5
  • What did you expect to find in _rawBody ? Can you add an example ? – mbesson Jun 02 '21 at 07:06
  • In addition to @mbesson's answer, there are a couple of other issues: 1) the path [must not include the query string](https://stackoverflow.com/q/30251585/113116) (`?method=...`); query parameters must be defined as `in: query` parameters instead; 2) mixup of OpenAPI 2.0 syntax (`consumes`, `in: body` parameters) and OpenAPI 3.0 syntax (`content.application/json` in responses). If you actually use OpenAPI 3.0, then you need to use [`requestBody . content . application/json`](https://swagger.io/docs/specification/describing-request-body/) instead of a body parameter. – Helen Jun 02 '21 at 07:21
  • You can [export your OpenAPI definition](https://stackoverflow.com/a/48525934/113116) from Swagger UI and paste it into https://editor.swagger.io to check for syntax errors. Make sure to fix any syntax errors. – Helen Jun 02 '21 at 07:21

3 Answers3

0

Change - in: data to - in: body

Source

mbesson
  • 629
  • 5
  • 24
0
swagger: "2.0"
info:
  description: "This is a sample server Petstore server.  You can find out more about     Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).      For this sample, you can use the api key `special-key` to test the authorization     filters."
  version: "1.0.0"
  title: "Swagger Petstore"
  termsOfService: "http://swagger.io/terms/"
  contact:
    email: "apiteam@swagger.io"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
basePath: "/api/v1/index/"
paths:
  /nocache:
    post:
      summary: "Frontend API"
      description: "Check If Custom Payment Link Is Valid For Given Days"
      consumes:
      - "application/json"
      - "application/xml"
      produces:
      - "application/xml"
      - "application/json"
      parameters:
        - in: "body"
          name: "body"
          description: "The body will contain order_id and valid_for"
          schema:
            type: object
            properties:
              order_id:
                type: string
              valid_for:
                type: integer
        - in: query
          name: method
          description: "The method=checkIfPaymentIsValidForDays will us in parameters"
          type: string
        
      responses:
        "200":
          description: "Response success or failure"
          schema:
            $ref: '#/definitions/ApiResponse'
            
definitions:
  ApiResponse:
    type: "object"
    properties:
      code:
        type: "integer"
        format: "int32"
      type:
        type: "string"
      message:
        type: "string"
externalDocs:
  description: "Find out more about Swagger"
  url: "http://swagger.io"
0

Change the content type from application/json to text/plain

/url/test:
 patch:
  tags:
    - Test
  summary: Test Request
  requestBody:
    content:
      text/plain:
        schema:
          type: object
          example:
            name: 'test-name'
Nishan B
  • 627
  • 7
  • 11