Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Avoid monkey patching the multipart-post gem to specify Content-Type headers #35

wants to merge 1 commit into from

2 participants


After digging into the multipart-post gem source I found out how to pass the headers options for a specific part and avoid monkey patching the gem.


This is great, thanks @skateinmars! I'll test it out and merge it in soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
2 
@@ -75,8 +75,6 @@ The above options allow you to change the endpoint (to be able to hit the produc
The docusign\_rest gem makes creating multipart POST (aka file upload) requests to the DocuSign REST API dead simple. It's built on top of Net:HTTP and utilizes the [multipart-post]( gem to assist with formatting the multipart requests. The DocuSign REST API requires that all files be embedded as JSON directly in the request body (not the body\_stream like multipart-post does by default) so the docusign\_rest gem takes care of [setting that up for you](
-This gem also monkey patches one small part of multipart-post to inject some header values and formatting that DocuSign requires. If you would like to see the monkey patched code please take a look at [lib/multipart-post/parts.rb]( It's only re-opening one method, but feel free to make sure you understand that code if it concerns you.
### Examples
* These examples assume you have already run the `docusign_rest:generate_config` rake task and have the configure block properly setup in an initializer with your username, password, integrator\_key, and account\_id.
1  lib/docusign_rest.rb
@@ -4,7 +4,6 @@
require_relative 'docusign_rest/utility'
require 'multipart_post' #require the multipart-post gem itself
require 'net/http/post/multipart' #require the multipart-post net/http/post/multipart monkey patch
-require_relative 'multipart_post/parts' #require my monkey patched parts.rb which adjusts the build_part method
require 'net/http'
require 'json'
3  lib/docusign_rest/client.rb
@@ -547,6 +547,9 @@ def initialize_net_http_multipart_post_request(uri, post_body, file_params, head
# headers={} - The fully merged, final request headers
# boundary - Optional: you can give the request a custom boundary
+ headers = headers.dup.merge(parts: {post_body: {'Content-Type' => 'application/json'}})
request =
{ post_body: post_body }.merge(file_params),
20 lib/multipart_post/parts.rb
@@ -1,20 +0,0 @@
-require 'multipart_post'
-require 'parts'
-Parts::ParamPart.class_eval do
- def build_part(boundary, name, value, headers = {})
- part = ""
- part << "--#{boundary}\r\n"
- # TODO (2014-02-03) jonk => multipart-post seems to allow for adding
- # a configurable header, hence the headers param in the method definition
- # above. However, I can't seem to figure out how to acctually get it passed
- # all the way through to line 42 of the Parts module in the parts.rb file.
- # So for now, we still monkeypatch the content-type in directly.
- part << "Content-Type: application/json\r\n"
- part << "Content-Disposition: form-data; name=\"#{name.to_s}\"\r\n"
- part << "\r\n"
- part << "#{value}\r\n"
- end
Something went wrong with that request. Please try again.