Skip to content

Content-Type improvements: requestContentType priority, + optional empty-body setter #1300

@shockey

Description

@shockey

From @shockey on April 25, 2018 5:27

  1. patch the requestContentType logic to always apply, regardless of the presence of a body/form value... because we should trust that the user knows what they want.
  2. add an attachContentTypeForEmptyPayload option (that can be leveraged through the constructor, buildRequest/execute, and the tags interface), which sets the Content-Type whenever a body/formData parameter/request body is defined, regardless of the provided content or lack thereof.

Demonstration

const spec = {
  "swagger": "2.0",
  "info": {},
  "host": "petstore.swagger.io",
  "basePath": "/v2",
  "schemes": ["http"],
  "consumes": ["application/json"],
  "paths": {
    "/": {
      "post": {
        "operationId": "myOp",
        "parameters": [
          {
            "name": "body",
            "in": "body",
            "schema": {
              "type": "object"
            }
          }
        ]
      }
    }
  }
}

const req1 = SwaggerClient.buildRequest({
  spec,
  pathName: "/",
  method: "post",
  operationId: "myOp",
  requestContentType: "application/x-custom",
  parameters: {
    body: null
  }
})

const req2 = SwaggerClient.buildRequest({
  spec,
  pathName: "/",
  method: "post",
  operationId: "myOp",
  attachContentTypeForEmptyPayload: true,
  parameters: {
    body: null
  }
})

Expected behavior

req1.headers === { "Content-Type": "application/x-custom" }
req2.headers === { "Content-Type": "application/json" }

Current behavior

req1.headers === {}
req2.headers === {}

Originally reported by @fehguy over email

Copied from original issue: swagger-api/swagger-ui#4475

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions