Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content-Length not being set for POST requests? #335

Closed
bsodmike opened this issue Apr 25, 2016 · 7 comments
Closed

Content-Length not being set for POST requests? #335

bsodmike opened this issue Apr 25, 2016 · 7 comments
Milestone

Comments

@bsodmike
Copy link

Hi,

I was integrating reCaptcha today and can into something curious, HTTP.post('https://www.google.com/recaptcha/api/siteverify', params: { secret: secret, response: response }) and got

<p><b>411.</b> <ins>That’s an error.</ins>\n  <p>POST requests require a <code>Content-length</code> header.  <ins>That’s all we know.</ins>\n"

I tried setting the content length to some random value but I found it would hang of I did,

HTTP['Content-Length': 500].post('https://www.google.com/recaptcha/api/siteverify', params: { secret: secret, response: response })

#...or

HTTP[content_length: 500].post('https://www.google.com/recaptcha/api/siteverify', params: { secret: secret, response: response })

I suppose, this is because the content length is invalid; I didn't have time prying into the library to figure this out as I've gotten HTTParty working just fine.

Thanks!

@tarcieri
Copy link
Member

You're correct we should be sending Content-Length if the length of the request body is known in advance. That said, in your case the request body is empty, so sending an arbitrary length like that will hang the remote server as it waits for the rest of the body.

@bsodmike
Copy link
Author

Ah no wonder, it's just strange that the reCaptcha server has this set. I wonder if we could add some log notification if the gem if waiting for rest of the body and there isn't any response after a certain timeout? It's just to potentially prevent others seeing something similar, even though I'd agree this is not a typical use-case.

Cheers @tarcieri

@tarcieri
Copy link
Member

This gem has extensive timeout support: https://github.com/httprb/http/wiki/Timeouts

@bsodmike
Copy link
Author

Ooh fantastic, thanks.

@bsodmike
Copy link
Author

This does the trick, thanks!

HTTP[content_length: 0]. \
  timeout(write: 2, connect: 5, read: 20). \
  post('https://www.google.com/recaptcha/api/siteverify', params: {
  secret: secret, response: response 
})

=> {"success"=>true,
 "challenge_ts"=>"2016-04-25T15:53:14Z",
 "hostname"=>"localhost"}

@tarcieri
Copy link
Member

Reopening as we should probably set the request Content-Length if it's known in advance.

@tarcieri tarcieri reopened this Apr 25, 2016
@tarcieri tarcieri added this to the v2.0 milestone Apr 25, 2016
@ixti ixti modified the milestones: v2.0, v2.x May 5, 2016
@ixti
Copy link
Member

ixti commented Aug 1, 2016

Resolved by #360

jsonn pushed a commit to jsonn/pkgsrc that referenced this issue Oct 18, 2016
## 2.0.3 (2016-08-03)

* [#365](httprb/http#365)
  Add `HTTP::Response#content_length`
  ([@janko-m])

* [#335](httprb/http#335),
  [#360](httprb/http#360)
  Set `Content-Length: 0` header for `nil` bodies.
  ([@britishtea])


## 2.0.2 (2016-06-24)

* [#353](httprb/http#353)
  Avoid a dependency cycle between Client and Connection classes.
  ([@jhbabon])


## 2.0.1 (2016-05-12)

* [#341](httprb/http#341)
  Refactor some string manipulations so they are more performant
  (up to 3-4x faster) and more concise.
  ([@tonyta])

* [#339](httprb/http#341)
  Always use byte methods when writing/slicing the write buffer.
  ([@zanker])


## 2.0.0 (2016-04-23)

* [#333](httprb/http#333)
  Fix HTTPS request headline when sent via proxy.
  ([@Connorhd])

* [#331](httprb/http#331)
  Add `#informational?`, `#success?`, `#redirect?`, `#client_error?` and
  `#server_error?` helpers to `Response::Status`.
  ([@mwitek])

* [#330](httprb/http#330)
  Support custom CONNECT headers (request/response) during HTTPS proxy requests.
  ([@smudge])

* [#319](httprb/http#319)
  Drop Ruby 1.9.x support.
  ([@ixti])


## 1.0.4 (2016-03-19)

* [#320](httprb/http#320)
  Fix timeout regression.
  ([@tarcieri])


## 1.0.3 (2016-03-16)

* [#314](httprb/http#314)
  Validate charset before forcing encoding.
  ([@kylekyle])

* [#318](httprb/http#318)
  Remove redundant string allocations upon header names normalization.
  ([@ixti])
@ixti ixti closed this as completed Dec 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants