GithubHelp home page GithubHelp logo

How do I pass POST payload? about down HOT 4 CLOSED

janko avatar janko commented on May 11, 2024
How do I pass POST payload?

from down.

Comments (4)

janko avatar janko commented on May 11, 2024

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.

Nakilon avatar Nakilon commented on May 11, 2024

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.

Nakilon avatar Nakilon commented on May 11, 2024

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.

jrochkind avatar jrochkind commented on May 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.