ruby http error - wrong Content-Length format - ruby-on-rails

I have the following code
url = URI.parse("http://localhost:3100/tasks/#{id}.xml")
http = Net::HTTP.new(url.host, url.port)
# Make request
response = http.start do |http|
http.get(url.request_uri)
end
Whenever this code is run, it returns the following error
Net::HTTPHeaderSyntaxError in TasksController#show
wrong Content-Length format
Does anyone know whats going wrong here?
Thanks,
Josh

Hmm, this code does not look right:
response = http.start do |http|
The variable you are passing to your block should not have the same name as an existing variable. You may want something more like what is in the docs.
require 'net/http'
url = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
puts res.body

Your code works when I point it to, for example, http://www.google.com, so I suspect that your server is spitting out something that gives Net::HTTP indigestion. Try adding this line just after your call to HTTP.new:
http.set_debug_output($stderr)
That should spew the entire transaction to the screen. I imagine you'll find something odd with the Content-Length field in the response header (Net::HTTP expects to find a string of digits in the that field). If it's not obvious from that what's the matter, then post that output (or at least the header portion of the response) here and let these smart guys have a go at it.

Related

rails http request with file on body

I am using trello api to attach an image to a card. the documentation says
require 'uri'
require 'net/http'
url = URI("https://api.trello.com/1/cards/id/attachments")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url)
response = http.request(request)
puts response.read_body
After putting my key and my token, I tried to upload a file and the binary data goes in the url itself, not only it seems too ugly but it also doesn't work because the request is really too long. I've tried using multipart and rest client gems from in my code to upload and attach a file to a trello card but everytime I get errors like bad request or SSL errors, can anyone please give me a piece of code that really works? thanks
actually I am sending the image data via AJAX (I'm generating it from a charjs view), so the data sent is binary, it would be better if the solution upload an image from binary data.
Their documentation does indeed encourage you to add the whole encoded file object into the URL, which I also find ugly. I wonder if it will work to add it into the POST body instead? Try this:
request = Net::HTTP::Post.new(url)
request.set_form_data({file: put_encoded_file_contents_here})

Using Net::HTTP to POST Json data to remote server

I'm Using ruby on rails and for some reason the remote server doesn't accept the format of my data, I want to make sure nothing else is wrong with with my code:
I have tried your code with following and it's returning data, you can ignore the line request.body or you can use it, in both cases it will return you the response.
uri=URI.parse('https://mkpartners.secure.force.com/services/apexrest/careers?firstName=ss&lastName=dd&email=pp#ss.com')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
request.body =JSON.pretty_generate(data)
response = http.request(request)
The issue with the return value they are returning HTML, when you will try to prase the response using JSON.parse(response.body), it's throwing error.you need to confirm with other team that they should return json.
Problem solved, it was a webservice problem.

Parsing HTTPS instead of HTTP in Rails

I know how to parse HTTP requrests using Net::HTTP. How do you parse HTTPS requests?
I've tried and it says as a response (one of the lines):
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
stox
Edited:
So to show you in a little bit more detail:
For example, here is the HTTP API
https://xxxxx.info/merchant/balance?password=$main_password
How do I use Net:http to run this? Or do I use something else?
Thanks
Stox
would be helpful if you showed us some code?
try setting these attributes
# assuming http is generated like this http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
# might need this as well?
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
You can achieve that by setting use_ssl to true before executing the request.
uri = URI(https://xxxxx.info/merchant/balance?password=$main_password)
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(req)
end
puts res.body
reference https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-HTTPS

403 Developer Inactive on an HTTP pull

I am trying to query some XML from a hotel database using Ruby, and am getting the results:
403 Developer Inactive
I used the code:
require 'net/http'
url = URI.parse('URL of the HTTP query')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
puts res.body
I would give the actual URL, but it contains the API key/etc...so I really cannot divulge it.
Is there anything wrong with the code or might it be on the company needing to activate the key?
Thanks
403 is the HTTP status code FORBIDDEN, which leads me to believe you have a problem authenticating your API request, maybe because of a wrong key or something.

Make a GET Request and receive parameters

I am working on a Ruby on Rails App.I have to make a GET request to following:
https://[#subdomain].chargify.com/subscriptions/[#subscription.id].json
The response received is going to be json. I am trying to achieve this by making the GET request using the 'net/http' library of ruby and my code is as follows :
res = Net::HTTP.get(URI.parse('https://[#subdomain].chargify.com/subscriptions/[#subscription_id].json'))
But I am getting an error which is :
bad URI(is not URI?):
https://[#subdomain].chargify.com/subscriptions/[#subscription_id].json
I am not sure where exactly am I making mistake with this. Any suggestions or help would be appreciated.
Thanks
Do you mean #{#subdomain} instead of [#subdomain]? That has to be inside of double quotes " in order to get interpolated properly.
By now you know that your error was that you didn't interpolate the string correctly. You should have specified #{#subdomain} rather than [#subdomain] and you need double quotes around the string
The problem that you now have is that you need to tell the http connection to use ssl. I use something like this ..
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
Two things:
The value of #subdomain needs to be passed using
#{#subdomain}
The connection is ssl
def net_http(uri)
h = Net::HTTP.new(uri.host, uri.port)
h.use_ssl=true
h
end
Then
request=net_http(URI.parse("https://# {#subdomain}.chargify.com/subscriptions/#{#subscription_id}.json"))
To perform get actions
request=net_http(URI.parse("https://# {#subdomain}.chargify.com/subscriptions/#{#subscription_id}.json")).start do |http|
http.get(URI.parse("whatever_url").path,{:params=>123})
end

Resources