Send request with body_stream and parameters

I am creating some kind of proxy. When I call some URL in the rack application, I forward this request to another URL.

The request I forward is a POST with a file and some parameters.
I want to add additional parameters.
But the file can be quite large. Therefore, I am sending it with Net::HTTP#body_streaminstead Net::HTTP#body.

I receive my request as an object Rack::Request, and I create my Net :: HTTP object with this.

req = Net::HTTP::Post.new(request.path_info)
req.body_stream = request.body
req.content_type = request.content_type
req.content_length = request.content_length

http = Net::HTTP.new(@host, @port)
res = http.request(req)

I tried several ways to add proxy settings. But there is nothing in Net :: HTTP that allows you to add parameters to the body_stream request, only to the unit of the body.

Is there an easier proxy way to request a rack? Or an easy way to add my parameters to my request?

+5
2

, , . , . Rack:: Request, ( , ), ( - ActionController:: ParamsParser), , StringIo. StringIO :

Content-Type: multipart/form-data; boundary=AaB03x
--AaB03x 
Content-Disposition: form-data; name="param1" 
value1
--AaB03x 
Content-Disposition: form-data; name="files"; filename="file1.txt" 
Content-Type: text/plain 
... contents of file1.txt ...
--AaB03x--

, Net:: HTTP, : (1). ; (2). ; (3). . , Net:: HTTP- (1), , .

, , , .

:

  • ActionController:: ParamsParser . rest-client lib, - :

    RestClient.post('http://your_server' + request.path_info),: params = > params.merge(your_params)

  • StringIO . .

+3

, , IPN Paypal. IPN Paypal , : cmd = > '_notify-validate'.

, , URL-, :

reply_request = Net::HTTP::Post.new(url.path + '?cmd=_notify-validate')

, , , , .

0

All Articles