Skip to content

Option to configure post body encoding for multipart/form-data #1480

@devth

Description

@devth

Q&A (please complete the following information)

  • OS: macOS
  • Environment: node 10.16.3
  • Method of installation: npm
  • Swagger-Client version: 3.5.2
  • Swagger/OpenAPI version: OpenAPI 3.0

Swagger/OpenAPI definition:

Partial spec relevant to the question:

requestBody: {
  content: {
    multipart/form-data: {
      schema: {
        properties: {
          email: {
            description: "Email address",
            type: "string"
          },
          password: {
            description: "Password",
            type: "string"
          }
        },
        type: "object"
      }
    }
  }
},

Swagger-Client usage:

const swaggerClientOpts = {
  timeout: 5000,
  showDebug: true,
  debugLevel: 'verbose',
  requestInterceptor: (req) => {
    console.log('req', _.omit(req, ['responseInterceptor', 'requestInterceptor']));
    return req;
  },
  responseInterceptor: (res) => {
    if (res.status !== 200) {
      // always log non-successful responses
      console.log('Non 200 response', res.status);
      console.log(JSON.stringify(res, null, 2));
    }
    return res;
  }
};

// lazily instantiate the client
let swaggerClient = undefined;
export const getClient = async () => {
  try {
    if (!swaggerClient) {
      swaggerClient = await Swagger(swaggerUrl, swaggerClientOpts);
    }
  } catch (e) {
    console.error('Swagger Client error', e);
  }
  return swaggerClient;
};

How can we help?

It looks like when an OpenAPI spec specifies multipart/form-data in the request body, swagger-js will encode the POST body as JSON, as indicated by this sample request.

Client code:

  (await (Authentication.authenticate({},
    {
      requestBody: {
        email: 'foo@bar.com',
        password: '123'
      }
    }
  )))

Resulting request (as logged by req interceptor):

{ url: 'https://omitted.com/authentication',
  credentials: 'same-origin',
  headers: { 'Content-Type': 'multipart/form-data' },
  method: 'POST',
  body: '{"email":"foo@bar.com","password":"123"}' }

I verified the exact request using ngrep as well:

POST /authentication HTTP/1.1.
accept-encoding: gzip,deflate.
user-agent: node-fetch/1.0 (+https://github.com/bitinn/node-fetch).
connection: close.
accept: */*.
content-length: 52.
Host: omitted.com.
.
{"email":"foo@bar.com","password":"123"}

Is there any configuration that controls the encoding of the POST body? Or are there any docs that specify which content-type maps to which encoding? I'm working against a server that only accepts formData key/value pairs e.g. email=foo@bar.com&password=123.

curl examples for reference

Both of these content types demonstrate non-JSON-encoded bodies by default.

multipart/form-data

curl -XPOST http://omitted.com/authentication \
  --header 'Content-Type: multipart/form-data' \
  -d email='foo@bar.com' \
  -d password='123'

Resulting req:

POST /authentication HTTP/1.1.
Host: omitted.com.
User-Agent: curl/7.54.0.
Accept: */*.
Content-Type: multipart/form-data.
Content-Length: 30.
.
email=foo@bar.com&password=123

Notice how this uses KV pairs & delimiter.

application/x-www-form-urlencoded

curl -XPOST http://omitted.com/authentication \
  -d email='foo@bar.com' \
  -d password='123'
POST /authentication HTTP/1.1.
Host: omitted.com.
User-Agent: curl/7.54.0.
Accept: */*.
Content-Length: 42.
Content-Type: application/x-www-form-urlencoded.
.
email=foo@bar.com&password=123

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions