From f19968d7b1bdcf50e4cc1a6b2f74aed25ff656f8 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Moal Date: Thu, 17 Jul 2014 11:59:27 +0200 Subject: [PATCH] Avoid monkey patching the multipart-post gem to specify Content-Type headers --- README.md | 2 -- lib/docusign_rest.rb | 1 - lib/docusign_rest/client.rb | 3 +++ lib/multipart_post/parts.rb | 20 -------------------- 4 files changed, 3 insertions(+), 23 deletions(-) delete mode 100644 lib/multipart_post/parts.rb diff --git a/README.md b/README.md index 0e69c527..cf80eb0d 100644 --- a/README.md +++ b/README.md @@ -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](https://github.com/nicksieger/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](https://github.com/j2fly/docusign_rest/blob/master/lib/docusign_rest/client.rb#L397). -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](https://github.com/j2fly/docusign_rest/blob/master/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. diff --git a/lib/docusign_rest.rb b/lib/docusign_rest.rb index 883b8566..12db69a2 100644 --- a/lib/docusign_rest.rb +++ b/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' diff --git a/lib/docusign_rest/client.rb b/lib/docusign_rest/client.rb index ebe097cc..d535d9f9 100644 --- a/lib/docusign_rest/client.rb +++ b/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 = Net::HTTP::Post::Multipart.new( uri.request_uri, { post_body: post_body }.merge(file_params), diff --git a/lib/multipart_post/parts.rb b/lib/multipart_post/parts.rb deleted file mode 100644 index 8d85dde6..00000000 --- a/lib/multipart_post/parts.rb +++ /dev/null @@ -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 -end