Simple POST request failing with em-http-request

985 views Asked by At

The following query works with requestmaker:

URI:

http://www.cleverbot.com/webservicemin/

Query:

start=y&icognoid=wsf&fno=0&sub=Say&islearning=1&cleanslate=false&stimulus=!!!%20there%20was%20an%20error%20!!!&icognocheck=af71393ce00d9126a247df2f53948e79

But it does not work with em-http-request:

require 'eventmachine'
require 'em-http-request'


uri  = 'http://www.cleverbot.com/webservicemin/'
query = 'start=y&icognoid=wsf&fno=0&sub=Say&islearning=1&cleanslate=false&stimulus=!!!%20there%20was%20an%20error%20!!!&icognocheck=af71393ce00d9126a247df2f53948e79'

EM.run do

  http = EM::HttpRequest.new(uri).post(query: query)
  http.callback { puts http.response; EM.stop }
  http.errback { puts 'There was an error'; EM.stop }
end

which prints There was an error. I feel stumped because this simple example works with any other method of sending a request and I've checked around to see if my usage was wrong but it doesn't seem to be.

Edit: Just for reference, this is not the correct way to use cleverbot. I made a second mistake by sending the data under :query. If you use http.post(body: query) it will work

1

There are 1 answers

0
igrigorik On BEST ANSWER

Looks like a badly implemented server: it aborts the TCP connection without returning a proper HTTP status code, which is why you see "connection closed by server" when you query http.error.

If you change the default user agent to curl's UA string, you get a response:

  http = EM::HttpRequest.new(uri).post({
   :query => query,
   :head => {'User-Agent' => 'curl/7.30.0'}
  })