0

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

itdoesntwork
  • 4,666
  • 3
  • 25
  • 38
  • You are getting `http.error == "connection closed by server"`. This could be because the wrong `query`- it probably does nothing to do with the eventmachine – Grych Aug 06 '14 at 15:58
  • I tried to POST your query directly - cleverbot responses with noting: `curl --data 'start=y&icognoid=wsf&fno=0&sub=Say&islearning=1&cleanslate=false&stimulus=!!!%20there%20was%20an%20error%20!!!&icognocheck=af71393ce00d9126a247df2f53948e79' http://www.cleverbot.com/webservicemin/` – Grych Aug 06 '14 at 15:58
  • @Grych That's odd, I pasted that into sh and got back a valid answer (cleverbot uses `\r` to separate its lines for some reason, maybe that's why you didn't see anything?) – itdoesntwork Aug 06 '14 at 20:47
  • Yeah, right, this is odd response :) maybe it is a reason why `HttpRequest` finds it a `connection closed by server` – Grych Aug 06 '14 at 20:55

1 Answers1

2

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'}
  })
igrigorik
  • 9,433
  • 2
  • 29
  • 30