{"id":13877976,"url":"https://github.com/socketry/multipart-post","last_synced_at":"2025-05-14T20:04:27.576Z","repository":{"id":422169,"uuid":"42073","full_name":"socketry/multipart-post","owner":"socketry","description":"Adds multipart POST capability to net/http","archived":false,"fork":false,"pushed_at":"2024-09-03T22:18:14.000Z","size":181,"stargazers_count":295,"open_issues_count":9,"forks_count":71,"subscribers_count":12,"default_branch":"main","last_synced_at":"2025-04-07T00:05:09.909Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/socketry.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":"2008-08-12T17:33:41.000Z","updated_at":"2025-03-19T11:47:14.000Z","dependencies_parsed_at":"2024-09-16T19:25:51.579Z","dependency_job_id":"9f15acc1-4255-4d90-a4aa-d88fce53b89b","html_url":"https://github.com/socketry/multipart-post","commit_stats":{"total_commits":168,"total_committers":40,"mean_commits":4.2,"dds":0.7261904761904762,"last_synced_commit":"cce3634876b74e08a6c51d02db00070a82dc8cc4"},"previous_names":["nicksieger/multipart-post"],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Fmultipart-post","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Fmultipart-post/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Fmultipart-post/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Fmultipart-post/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/socketry","download_url":"https://codeload.github.com/socketry/multipart-post/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248643010,"owners_count":21138354,"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-06T08:01:36.519Z","updated_at":"2025-04-14T02:58:14.657Z","avatar_url":"https://github.com/socketry.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Multipart::Post\n\nAdds a streamy multipart form post capability to `Net::HTTP`. Also supports other\nmethods besides `POST`.\n\n[![Development Status](https://github.com/socketry/multipart-post/workflows/Test/badge.svg)](https://github.com/socketry/multipart-post/actions?workflow=Test)\n\n## Features/Problems\n\n  - Appears to actually work. A good feature to have.\n  - Encapsulates posting of file/binary parts and name/value parameter parts, similar to\n    most browsers' file upload forms.\n  - Provides an `UploadIO` helper class to prepare IO objects for inclusion in the params\n    hash of the multipart post object.\n\n## Installation\n\n``` shell\nbundle add multipart-post\n```\n\n## Usage\n\n``` ruby\nrequire 'net/http/post/multipart'\n\nurl = URI.parse('http://www.example.com/upload')\nFile.open(\"./image.jpg\") do |jpg|\n  req = Net::HTTP::Post::Multipart.new url.path,\n    \"file\" =\u003e UploadIO.new(jpg, \"image/jpeg\", \"image.jpg\")\n  res = Net::HTTP.start(url.host, url.port) do |http|\n    http.request(req)\n  end\nend\n```\n\nTo post multiple files or attachments, simply include multiple parameters with\n`UploadIO` values:\n\n``` ruby\nrequire 'net/http/post/multipart'\n\nurl = URI.parse('http://www.example.com/upload')\nreq = Net::HTTP::Post::Multipart.new url.path,\n  \"file1\" =\u003e UploadIO.new(File.new(\"./image.jpg\"), \"image/jpeg\", \"image.jpg\"),\n  \"file2\" =\u003e UploadIO.new(File.new(\"./image2.jpg\"), \"image/jpeg\", \"image2.jpg\")\nres = Net::HTTP.start(url.host, url.port) do |http|\n  http.request(req)\nend\n```\n\nTo post files with other normal, non-file params such as input values, you need to pass hashes to the `Multipart.new` method.\n\nIn Rails 4 for example:\n\n``` ruby\ndef model_params\n  require_params = params.require(:model).permit(:param_one, :param_two, :param_three, :avatar)\n  require_params[:avatar] = model_params[:avatar].present? ? UploadIO.new(model_params[:avatar].tempfile, model_params[:avatar].content_type, model_params[:avatar].original_filename) : nil\n  require_params\nend\n\nrequire 'net/http/post/multipart'\n\nurl = URI.parse('http://www.example.com/upload')\nNet::HTTP.start(url.host, url.port) do |http|\n  req = Net::HTTP::Post::Multipart.new(url, model_params)\n  key = \"authorization_key\"\n  req.add_field(\"Authorization\", key) #add to Headers\n  http.use_ssl = (url.scheme == \"https\")\n  http.request(req)\nend\n```\n\nOr in plain ruby:\n\n``` ruby\ndef params(file)\n  params = { \"description\" =\u003e \"A nice picture!\" }\n  params[:datei] = UploadIO.new(file, \"image/jpeg\", \"image.jpg\")\n  params\nend\n\nurl = URI.parse('http://www.example.com/upload')\nFile.open(\"./image.jpg\") do |file|\n  req = Net::HTTP::Post::Multipart.new(url.path, params(file))\n  res = Net::HTTP.start(url.host, url.port) do |http|\n    return http.request(req).body\n  end\nend\n```\n\n### Parts Headers\n\nBy default, all individual parts will include the header `Content-Disposition` as well as `Content-Length`, `Content-Transfer-Encoding` and `Content-Type` for the File Parts.\n\nYou may optionally configure the headers `Content-Type` and `Content-ID` for both ParamPart and FilePart by passing in a `parts` header.\n\nFor example:\n\n``` ruby\nurl = URI.parse('http://www.example.com/upload')\n\nparams = {\n  \"file_metadata_01\" =\u003e { \"description\" =\u003e \"A nice picture!\" },\n  \"file_content_01\"  =\u003e UploadIO.new(file, \"image/jpeg\", \"image.jpg\")\n}\n\nheaders = {\n  'parts': {\n    'file_metadata_01': {\n      'Content-Type' =\u003e \"application/json\"\n      }\n    }\n  }\n\nreq = Net::HTTP::Post::Multipart.new(uri, params, headers)\n```\n\nThis would configure the `file_metadata_01` part to include `Content-Type`\n\n    Content-Disposition: form-data; name=\"file_metadata_01\"\n    Content-Type: application/json\n      {\n        \"description\" =\u003e \"A nice picture!\" \n      }\n\n#### Custom Parts Headers\n\n*For FileParts only.*\n\nYou can include any number of custom parts headers in addition to `Content-Type` and `Content-ID`.\n\n``` ruby\nheaders = {\n  'parts': {\n    'file_metadata_01': {\n      'Content-Type' =\u003e \"application/json\",\n      'My-Custom-Header' =\u003e \"Yo Yo!\"\n    }\n  }\n}\n```\n\n### Debugging\n\nYou can debug requests and responses (e.g. status codes) for all requests by adding the following code:\n\n``` ruby\nhttp = Net::HTTP.new(uri.host, uri.port)\nhttp.set_debug_output($stdout)\n```\n\n## Versioning\n\nThis library aims to adhere to [Semantic Versioning 2.0.0](http://semver.org/).\nViolations of this scheme should be reported as bugs. Specifically,\nif a minor or patch version is released that breaks backward\ncompatibility, a new version should be immediately released that\nrestores compatibility. Breaking changes to the public API will\nonly be introduced with new major versions.\n\nAs a result of this policy, you can (and should) specify a\ndependency on this gem using the [Pessimistic Version Constraint](http://guides.rubygems.org/patterns/#pessimistic-version-constraint) with two digits of precision.\n\nFor example:\n\n``` ruby\nspec.add_dependency 'multipart-post', '~\u003e 2.1'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketry%2Fmultipart-post","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsocketry%2Fmultipart-post","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketry%2Fmultipart-post/lists"}