Skip to content

Conversation

erunion
Copy link
Member

@erunion erunion commented Aug 8, 2020

🧰 What's being changed?

  • Adds general support for multipart/form-data requests and the ability to accept file uploads.
  • Updates SDK snippets to comply with these changes.

In order to upload a file as part of a multipart/form-data request (or one of its supported variants), you can just give a path to the file as the value of the property in the parameters for the request. For example:

sdk.post('/anything', { documentFile: 'owlbert.png' })
  .then(res => res.json())
  .then(res => {
    console.log(res);
  });

This will automatically locate owlbert.png, read it into memory, convert it into data URL, and hand it off to fetch-har.

You can also give it a stream, and the SDK will handle everything as well like magic.

sdk.post('/anything', { documentFile: fs.createReadStream('owlbert.png') })
  .then(res => res.json())
  .then(res => {
    console.log(res);
  });

Resolves #131 and #130

@erunion erunion added enhancement New feature or request area:core Issues related to `core`, which is the package that powers the SDKs at runtime area:snippets Issues related to code snippets labels Aug 8, 2020
@erunion erunion marked this pull request as ready for review August 14, 2020 00:52
Copy link
Member

@domharrington domharrington left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💪

@erunion
Copy link
Member Author

erunion commented Aug 14, 2020

Holding off on merging this in until next week because after running some tests against our own API, fetch-har still needs some work.

{ error: 'SPEC_INVALID',
  message:
   'The spec you uploaded isn\'t a valid JSON or YAML file. Here\'s the error we found: Not valid YAML',
  suggestion:
   'Try running your spec through a validator to see if they pick up any issues with the formatting!',
  docs:
   'https://docs.readme.com/developers/logs/********',
  help:
   'If you need help, email support@readme.io and include the following link to your API log: \'https://docs.readme.com/developers/logs/********\'.',
  poem:
   [ 'When we asked for your swagger',
     'It seems you misunderstood',
     'You sent us a cool attitude',
     'Instead some JSON would have been good' ] }

Turns out sending a data URL instead of file contents doesn't actually work in real-world scenarios.

@erunion erunion merged commit 8f28341 into master Aug 17, 2020
@erunion erunion deleted the feat/support-multipart branch August 17, 2020 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core Issues related to `core`, which is the package that powers the SDKs at runtime area:snippets Issues related to code snippets enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support multipart/form-data operations

2 participants