5

I am working on rails environment. I am using Net::HTTP module for calling an external API and getting the response. This is working fine in my local host. But in staging it throwing an Net::HTTPBadResponse error. My staging is SSL enabled. This is the difference. Providing the code snippet and error below.

parameters = {'VirtualNumber' => '09845xxxxxx','Number[]' => "09878xxxxxx" }
x = Net::HTTP.post_form(URI.parse("https://example.com"), parameters)

Error:
Net::HTTPBadResponse (wrong status line: "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">")

The successful result will be in XML format. Can any one help me to solve this.

Thank You,
Regards

apr
  • 676
  • 1
  • 9
  • 21
  • I would suggest that the server is sending data back without HTTP header, e.g. only the body. So instead of ` – Steffen Ullrich May 05 '14 at 08:59
  • Hi,I didn't get you exactly. Can you explain little more? – apr May 05 '14 at 10:27
  • A HTTP message consists of a header and a body, the first line of the header is the status line, which looks like "HTTP/1.1 200 ok" etc. Net::HTTP complains about an invalid status line, because it looks like that your server did only send the HTTP body, w/o the HTTP header. So something is probably wrong with your server. – Steffen Ullrich May 05 '14 at 10:58
  • Thank you for your time. My problem has been solved. Giving my solution as answer below. – apr May 06 '14 at 12:01

1 Answers1

2

The issue was calling the API from ssl enabled site. So while calling the API we need to enable the ssl. And along with that need to provide basic authentication.

parameters = {'VirtualNumber' => '09845xxxxxx','Number[]' => "09878xxxxxx" }
url = URI.parse("https://example.com")
request = Net::HTTP::Post.new(url.path)
request.basic_auth "user", "pass"
request.set_form_data(parameters)
sock = Net::HTTP.new(url.host, url.port)
if url.scheme == 'https'
  sock.use_ssl = true
end
response = sock.start {|http| http.request(request) }
apr
  • 676
  • 1
  • 9
  • 21
  • I am facing similar issue as yours but I don't have access to staging server. I am getting this error only when I am posting huge data (> 1 lakh characters) in a rails form. Could you tell me what configs need to be done on server side. I have also posted my query here : (http://stackoverflow.com/questions/27146412/nethttpbadresponse-wrong-status-line-html-when-posting-huge-form-data). It would be nice if you could answer that too. Thanks. – furiabhavesh Dec 01 '14 at 07:40
  • Hi, As per my knowledge there is no size limitation for POST. because we are sending data as parameter list,not as url. As you mentioned in your question you are using an external API,please check if there any character limitation in your API description field. – apr Dec 03 '14 at 12:21
  • There is no limitation in the field. We will be soon getting access to staging server logs. Now, at-least it is sure that its a server side issue since there was nothing buggy from client side. Thanks for your reply. – furiabhavesh Dec 03 '14 at 15:23