@@ -42,14 +42,16 @@ gem install faraday-multipart
42
42
First of all, you'll need to add the multipart middleware to your Faraday connection:
43
43
44
44
``` ruby
45
+ require ' faraday'
45
46
require ' faraday/multipart'
46
47
47
48
conn = Faraday .new (...) do |f |
48
- f.request :multipart
49
+ f.request :multipart , ** options
49
50
# ...
50
51
end
51
52
```
52
53
54
+
53
55
Payload can be a mix of POST data and multipart values.
54
56
55
57
``` ruby
@@ -91,6 +93,50 @@ payload[:raw_with_id] = Faraday::Multipart::ParamPart.new(
91
93
conn.post(' /' , payload)
92
94
```
93
95
96
+ ### Sending an array of documents
97
+
98
+ Sometimes, the server you're calling will expect an array of documents or other values for the same key.
99
+ The ` multipart ` middleware will automatically handle this scenario for you:
100
+
101
+ ``` ruby
102
+ payload = {
103
+ files: [
104
+ Faraday ::Multipart ::FilePart .new (__FILE__ , ' text/x-ruby' ),
105
+ Faraday ::Multipart ::FilePart .new (__FILE__ , ' text/x-pdf' )
106
+ ],
107
+ url: [
108
+ ' http://mydomain.com/callback1' ,
109
+ ' http://mydomain.com/callback2'
110
+ ]
111
+ }
112
+
113
+ conn.post(url, payload)
114
+ # => POST url[]=http://mydomain.com/callback1&url[]=http://mydomain.com/callback2
115
+ # => and ncludes both files in the request under the `files[]` name
116
+ ```
117
+
118
+ However, by default these will be sent with ` files[] ` key and the urls with ` url[] ` , similarly to arrays in url parameters.
119
+ Some servers (e.g. Mailgun) expect each document to have the same parameter key insted.
120
+ You can instruct the ` multipart ` middleware to do so by providing the ` flat_encode ` option:
121
+
122
+ ``` ruby
123
+ require ' faraday'
124
+ require ' faraday/multipart'
125
+
126
+ conn = Faraday .new (...) do |f |
127
+ f.request :multipart , flat_encode: true
128
+ # ...
129
+ end
130
+
131
+ payload = ... # see example above
132
+
133
+ conn.post(url, payload)
134
+ # => POST url=http://mydomain.com/callback1&url=http://mydomain.com/callback2
135
+ # => and ncludes both files in the request under the `files` name
136
+ ```
137
+
138
+ This works for both ` UploadIO ` and normal parameters alike.
139
+
94
140
## Development
95
141
96
142
After checking out the repo, run ` bin/setup ` to install dependencies.
0 commit comments