Comments (4)
Passing form-encoded POST parameters can be done using the http.rb's :form
option. Example:
require "down/http"
require "json"
io = Down::Http.open("https://httpbin.org/post", method: :post, form: { "foo" => "bar" })
JSON.parse(io.read)
# => {"args"=>{},
# "data"=>"",
# "files"=>{},
# "form"=>{"foo"=>"bar"},
# "headers"=>
# {"Content-Length"=>"7",
# "Content-Type"=>"application/x-www-form-urlencoded",
# "Host"=>"httpbin.org",
# "User-Agent"=>"Down/5.2.1",
# "X-Amzn-Trace-Id"=>"Root=1-60abf327-318b8db9456ae63260a21bc9"},
# "json"=>nil,
# "origin"=>"83.240.62.43",
# "url"=>"https://httpbin.org/post"}
from down.
Oh, and I see JSON example there too, thanks. Somehow couldn't find it neither in tests nor in the main README.
UPD: ah sure, because that's another gem...
from down.
So I've got 411 Length Required (Down::ClientError)
. Passing "Content-Length"
header didn't help, so I started a fake server to inspect the requests:
require "webrick"
require "pp"
WEBrick::HTTPServer.new(Port: 8080).tap{ |s|
s.mount_proc("/"){ |req, res|
pp req.header; res.body = "1"*1000 + "2"*2000
}
}.start
require "down/http"
json = JSON.dump input
Down::Http.download(
"http://localhost:8080/",
headers: {
"Authorization" => "bearer 123}",
"Content-Length" => json.size
},
method: :post, body: json, max_size: 1000
).read
But it throws file is too large (max is 0MB) (Down::TooLarge)
and I see no way to get those "first 1000 bytes" that I need. So maybe the down gem wasn't supposed to do such thing in the first place and I'm in the dead end? I just wanted to limit the download size.
UPD: I'll start it as another ticket if needed.
from down.
I don't think setting content-length on an HTTP request does what you think it does, just in terms of the HTTP protocol. I don't think you are encountering a bug in down. Also I don't think this is about passing a POST payload anymore?
If you want to download only the first 1000 bytes of a server response, my ideas would be to try to use HTTP Range headers (assuming the server supports this), or you can perhaps use down's streaming API to only read the first 1000 bytes, and discard the rest.
Setting content-length in a request is not a way to do what you want, that is not how it works according to the HTTP protocol. Content-length on a request must be the size of the request body. This is not about down, just about the HTTP protocol. Also, how to download only first 1000 bytes of a response is a different question than passing a POST payload, no?
from down.
Related Issues (20)
- Down::Http can't get an S3 URL that Down::NetHttp can; 403 error HOT 2
- 304 Not Modified leads to redirect ArgumentError HOT 2
- arg with destination return a path? HOT 4
- Download via NetHttp and Cookie header fails HOT 2
- Progressbar Implementation HOT 1
- Support for Http 5.0 HOT 1
- undefined method `max_size=' when using Down.backend :http HOT 4
- Error: Down::SSLError (SSL_connect returned=1 errno=0 state=error: certificate verify failed (unable to get local issuer certificate)) HOT 2
- remote_file.read does not read full file for >1GB files HOT 6
- unknown method byte_size HOT 1
- 308 Permanent Redirect HOT 5
- Down::ChunkedIO#pos returns wildly incorrect values HOT 2
- `NoMethodError` trying to raise non-successful response `Down::ResponseError`
- Content-Disposition handling with non-standard header from IIS 10.0 returns `;` as part of filename HOT 2
- Compatibility with PDF::Reader HOT 2
- Ruby 3.2 unrecognized option: max_redirects HOT 3
- is Down gem thread safe? HOT 1
- gzipped responses are no decompressed with chunkedio
- Render saved file in Rails view HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from down.