Using Ruby, what is the most efficient way to get the content type of a given URL?
This is what I'd do if I want simple code:
require 'open-uri'
str = open('http://example.com')
str.content_type #=> "text/html"
The big advantage is it follows redirects.
If you're checking a bunch of URLs you might want to call close
on the handles after you've found what you want.
How to get content type of a given url inside Javascript?
Atlast I could figure out the whole thing with the great help of @Ian. Here is my completed code : In javascript file :
$("#wiki_form_url").change(function () {
$.ajax({
type: "GET",
url: "/wiki_forms/content",
data: {
input_url: $("#wiki_form_url").val()
},
dataType: "text"
}).done(function (data) {
// `data` contains the content-type
alert('Success');
console.log(data);
// alert(data);
}).fail(function () {
alert("failed AJAX call");
});
});
Inside my wiki_forms controller I created a new method named content :
def content
req = open(params[:input_url])
render :text => req.content_type
end
Then added a new route in routes.rb file :
get "/wiki_forms/content" => 'wiki_forms#content'
and used /wiki_forms/content as the ajax request url. And, everything is working nicely now.
Get Content Type of Request
I would usually go for request.format
and request.content_type
for reading these header fields.
EDIT: found a bit more on this that might help: https://stackoverflow.com/a/1595453/624590
How can I get content type from an ActiveStorage attachment?
Yes, there is a shorter solution: @video.source_blob.content_type
.
I recommend you to look at the source code of ActiveStorage, there you can see all the available methods and possibilities that are not always well documented.
How to send url-encoded form data using Faraday's post method?
The issue is that the parameters should start with capital letters. Your Faraday request is otherwise correct, but your form_data
method should look like:
def form_data
{
From: "whatsapp:+5491112312312",
Body: "Hello. Your order is on the way",
To: "whatsapp:+541132132121",
}
end
Verifying a remote image is actually an image file in ruby?
I would check to see if the service returns the proper mime types in the Content-Type HTTP header. (here's a list of mime types)
For example, the Content-Type of the StackOverflow homepage is text/html; charset=utf-8
, and the Content-Type of your gravatar image is image/png
To check the Content-Type header for image
in ruby using Net::HTTP, you would use the following:
def remote_file_exists?(url)
url = URI.parse(url)
Net::HTTP.start(url.host, url.port) do |http|
return http.head(url.request_uri)['Content-Type'].start_with? 'image'
end
end
Is it possible to download files from Filetype fields using Ruby?
Since you didn't give a valid URL, it's difficult to test solutions for you.
In general, retrieving the content of a URL is the same, whether it's a page or a file. Ruby's built-in OpenURI
is the fast path:
require 'open-uri'
file = open('http://example.com').read
Saving that file is easy:
IO.binwrite('/path/to/file_to_save', file)
Using binwrite
avoids any line-end translations that would occur saving binary data. For text data use:
IO.write('/path/to/file_to_save', file)
Both IO.binwrite
and IO.write
are documented in the IO module.
Related Topics
What Is the Advantage of Creating an Enumerable Object Using To_Enum in Ruby
Behaviour of Array Bang Methods
How to Generate a Unique Request Id in Rails
Given a Url, How to Get Just the Domain
Using Form_For Tag with Get Method
Monitor Ruby Processes with Monit
Catching Command-Line Errors Using %X
Should Gemfile.Lock Be Committed to Source Control on Windows
How to Prevent Rails Controller Generator to Modify Config/Routes.Rb
Using Fork in Windows with Ruby
How Is Ruby Tcpsocket Timeout Defined
How to Access Current_User Object in Model
How to Execute 2 or More Commands in the Same Ssh Session
Ruby: Cannot Install Watir Gem on Windows
Ruby Private Attr_Accessor and Unexpected Nil
Triple Single Quote VS Triple Double Quote in Ruby