Jwan622
Jwan622

Reputation: 11649

Some issues and odd error messages using net/http library in Ruby. What do the responses mean?

I have this code finally working:

res = Net::HTTP.post_form(URI('http://jobcoin.gemini.com/protozoan/api/transactions'), {})
=> #<Net::HTTPBadRequest 400 Bad Request readbody=true>
res.body
=> "{\"error\":{\"amount\":[\"This field is required\"],\"fromAddress\":[\"This field is required\"],\"toAddress\":[\"This field is required\"]}}"

I am able to make the post request to the server even though I have an error.

  1. What does readbody=true mean?

This does not work. Why? What does the error message mean?

Net::HTTP.post_form(URI('http://www.jobcoin.gemini.com/protozoan/api/transactions'), {})
SocketError: getaddrinfo: nodename nor servname provided, or not known

It seems like the www causes this to fail. Why is this? When I visit the site via a browser, the site does not exist. I always thought the two (with the www and without the www) were the same address. Is it not?

  1. What does nodename or socketerror mean exactly?

Upvotes: 0

Views: 369

Answers (2)

yorodm
yorodm

Reputation: 4461

There are several things here:

  1. You are making an wrong request to an API, that's why you're getting the #<Net::HTTPBadRequest 400 Bad Request readbody=true>. According to the response body you're missing some required parameters.
  2. The SocketError means you're trying to connect to a non-existing host.

Now to your questions:

  1. readbody=true means the server response was read. Check net/response.rb line 197.
  2. Think I've already answered some of your doubts with SocketError, nodename and servname refer to the domain name of the server.

Hope it helps.

Upvotes: 1

Rustam Gasanov
Rustam Gasanov

Reputation: 15791

The message you get after executing post_form is coming from the core ruby class which represents the http response:

def inspect
  "#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
end

This outputs the instance variable @read which(if I understand correctly) is a flag telling if the body was read.

The second question is too broad. In short words you can serve www or "naked domain" or both. Usually people redirect requests from www to the "naked domain" or visa-versa. In your case seems like the server doesn't serve www hence the error. The error message is pretty self-explanatory - "there is no node(computer) which serves this endpoint" - read about sockets.

Upvotes: 1

Related Questions