{"id":13531695,"url":"https://github.com/lostisland/faraday-multipart","last_synced_at":"2025-12-25T19:04:27.140Z","repository":{"id":38308993,"uuid":"443867084","full_name":"lostisland/faraday-multipart","owner":"lostisland","description":"Perform multipart-post requests using Faraday.","archived":false,"fork":false,"pushed_at":"2024-12-17T17:54:58.000Z","size":34,"stargazers_count":20,"open_issues_count":2,"forks_count":5,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-31T17:06:45.561Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lostisland.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-02T20:50:06.000Z","updated_at":"2025-03-25T01:37:00.000Z","dependencies_parsed_at":"2024-12-21T08:09:31.668Z","dependency_job_id":"d8e47c2b-34fd-449d-9823-d7a07689c0d5","html_url":"https://github.com/lostisland/faraday-multipart","commit_stats":{"total_commits":23,"total_committers":4,"mean_commits":5.75,"dds":0.4347826086956522,"last_synced_commit":"dd09ff002cfcda55e54f3656f0c7f2f7c958406d"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostisland%2Ffaraday-multipart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostisland%2Ffaraday-multipart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostisland%2Ffaraday-multipart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostisland%2Ffaraday-multipart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lostisland","download_url":"https://codeload.github.com/lostisland/faraday-multipart/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247226211,"owners_count":20904465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T07:01:04.939Z","updated_at":"2025-12-25T19:04:27.111Z","avatar_url":"https://github.com/lostisland.png","language":"Ruby","funding_links":[],"categories":["Middleware"],"sub_categories":[],"readme":"# Faraday Multipart\n\n[![ci](https://github.com/lostisland/faraday-multipart/actions/workflows/ci.yml/badge.svg)](https://github.com/lostisland/faraday-multipart/actions/workflows/ci.yml)\n[![Gem](https://img.shields.io/gem/v/faraday-multipart.svg?style=flat-square)](https://rubygems.org/gems/faraday-multipart)\n[![License](https://img.shields.io/github/license/lostisland/faraday-multipart.svg?style=flat-square)](LICENSE.md)\n\nThe `Multipart` middleware converts a `Faraday::Request#body` Hash of key/value pairs into a multipart form request, but\nonly under these conditions:\n\n* The request's Content-Type is \"multipart/form-data\"\n* Content-Type is unspecified, AND one of the values in the Body responds to\n  `#content_type`.\n\nFaraday contains a couple helper classes for multipart values:\n\n* `Faraday::Multipart::FilePart` wraps binary file data with a Content-Type. The file data can be specified with a String path to a\n  local file, or an IO object.\n* `Faraday::Multipart::ParamPart` wraps a String value with a Content-Type, and optionally a Content-ID.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'faraday-multipart'\n```\n\nAnd then execute:\n\n```shell\nbundle install\n```\n\nOr install it yourself as:\n\n```shell\ngem install faraday-multipart\n```\n\n## Usage\n\nFirst of all, you'll need to add the multipart middleware to your Faraday connection:\n\n```ruby\nrequire 'faraday'\nrequire 'faraday/multipart'\n\nconn = Faraday.new(...) do |f|\n  f.request :multipart, **options\n  # ...\nend\n```\n\nIf you need to [specify a different content type for the multipart\nrequest](https://www.iana.org/assignments/media-types/media-types.xhtml#multipart),\nyou can do so by providing the `content_type` option but it must start with\n`multipart/`\notherwise it will default to `multipart/form-data`:\n\n```ruby\nconn = Faraday.new(...) do |f|\n  f.request :multipart, content_type: 'multipart/mixed'\n  # ...\nend\n```\n\nPayload can be a mix of POST data and multipart values.\n\n```ruby\n# regular POST form value\npayload = { string: 'value' }\n\n# filename for this value is File.basename(__FILE__)\npayload[:file] = Faraday::Multipart::FilePart.new(__FILE__, 'text/x-ruby')\n\n# specify filename because IO object doesn't know it\npayload[:file_with_name] = Faraday::Multipart::FilePart.new(\n  File.open(__FILE__),\n  'text/x-ruby',\n  File.basename(__FILE__)\n)\n\n# Sets a custom Content-Disposition:\n# nil filename still defaults to File.basename(__FILE__)\npayload[:file_with_header] = Faraday::Multipart::FilePart.new(\n  __FILE__,\n  'text/x-ruby',\n  nil,\n  'Content-Disposition' =\u003e 'form-data; foo=1'\n)\n\n# Upload raw json with content type\npayload[:raw_data] = Faraday::Multipart::ParamPart.new(\n  { a: 1 }.to_json,\n  'application/json'\n)\n\n# optionally sets Content-ID too\npayload[:raw_with_id] = Faraday::Multipart::ParamPart.new(\n  { a: 1 }.to_json,\n  'application/json',\n  'foo-123'\n)\n\nconn.post('/', payload)\n```\n\n### Sending an array of documents\n\nSometimes, the server you're calling will expect an array of documents or other values for the same key.\nThe `multipart` middleware will automatically handle this scenario for you:\n\n```ruby\npayload = {\n  files: [\n    Faraday::Multipart::FilePart.new(__FILE__, 'text/x-ruby'),\n    Faraday::Multipart::FilePart.new(__FILE__, 'text/x-pdf')\n  ],\n  url: [\n    'http://mydomain.com/callback1',\n    'http://mydomain.com/callback2'\n  ]\n}\n\nconn.post(url, payload)\n#=\u003e POST url[]=http://mydomain.com/callback1\u0026url[]=http://mydomain.com/callback2\n#=\u003e   and includes both files in the request under the `files[]` name\n```\n\nHowever, by default these will be sent with `files[]` key and the URLs with `url[]`, similarly to arrays in URL parameters.\nSome servers (e.g. Mailgun) expect each document to have the same parameter key instead.\nYou can instruct the `multipart` middleware to do so by providing the `flat_encode` option:\n\n```ruby\nrequire 'faraday'\nrequire 'faraday/multipart'\n\nconn = Faraday.new(...) do |f|\n  f.request :multipart, flat_encode: true\n  # ...\nend\n\npayload = ... # see example above\n\nconn.post(url, payload)\n#=\u003e POST url=http://mydomain.com/callback1\u0026url=http://mydomain.com/callback2\n#=\u003e   and includes both files in the request under the `files` name\n```\n\nThis works for both `UploadIO` and normal parameters alike.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies.\n\nThen, run `bin/test` to run the tests.\n\nTo install this gem onto your local machine, run `rake build`.\n\n### Releasing a new version\n\nTo release a new version, make a commit with a message such as \"Bumped to 0.0.2\", and change the _Unreleased_ heading in `CHANGELOG.md` to a heading like \"0.0.2 (2022-01-01)\", and then use GitHub Releases to author a release. A GitHub Actions workflow then publishes a new gem to [RubyGems.org](https://rubygems.org/gems/faraday-multipart).\n\n## Contributing\n\nBug reports and pull requests are welcome on [GitHub](https://github.com/lostisland/faraday-multipart).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flostisland%2Ffaraday-multipart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flostisland%2Ffaraday-multipart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flostisland%2Ffaraday-multipart/lists"}