Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
devth opened this issue Oct 12, 2019 · 2 comments · Fixed by #1527
Closed

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

devth opened this issue Oct 12, 2019 · 2 comments · Fixed by #1527
Assignees

Comments

@devth
Copy link

devth commented Oct 12, 2019

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
@tim-lai tim-lai self-assigned this May 6, 2020
@tim-lai
Copy link
Contributor

tim-lai commented May 13, 2020

@devth Thanks for posting this issue. PR#1527 should resolve this issue.

@devth
Copy link
Author

devth commented May 14, 2020

@tim-lai awesome, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants