How do I read the header without reading the rest of the HTTP resource?
I was sure that when I do like this:
http = Net::HTTP.start uri.host, uri.port
request = Net::HTTP::Get.new uri
response = http.request request
I get some sort of established connection to the remote HTTP resource to be able to tell, for example, its Content-Type, while to really load the whole resource I then call the response.body.
But either I was always wrong or it is something with the server I access right now, the http.request loads the whole remote file that is unacceptable for me:
[Net::HTTP debug] opening connection to v.redd.it:80...
[Net::HTTP debug] opened
[Net::HTTP debug] <- "GET /6otzwem1c7721/DASH_9_6_M?source=fallback HTTP/1.1rnAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3rnAccept: */*rnUser-Agent: RubyrnHost: v.redd.itrnConnection: closernrn"
[Net::HTTP debug] -> "HTTP/1.1 200 OKrn"
[Net::HTTP debug] -> "Last-Modified: Sat, 29 Dec 2018 11:25:57 GMTrn"
[Net::HTTP debug] -> "ETag: "662291aec20b252aaebcf54c3b1827af-42"rn"
[Net::HTTP debug] -> "Content-Type: video/mp4rn"
[Net::HTTP debug] -> "Cache-Control: public, max-age=604800, s-maxage=86400, must-revalidatern"
[Net::HTTP debug] -> "Accept-Ranges: bytesrn"
[Net::HTTP debug] -> "Content-Length: 218756120rn"
[Net::HTTP debug] -> "Accept-Ranges: bytesrn"
[Net::HTTP debug] -> "Date: Sun, 30 Dec 2018 13:44:21 GMTrn"
[Net::HTTP debug] -> "Via: 1.1 varnishrn"
[Net::HTTP debug] -> "Connection: closern"
[Net::HTTP debug] -> "X-Served-By: cache-fra19120-FRArn"
[Net::HTTP debug] -> "X-Cache: HITrn"
[Net::HTTP debug] -> "X-Cache-Hits: 0rn"
[Net::HTTP debug] -> "X-Timer: S1546177461.284280,VS0,VE0rn"
[Net::HTTP debug] -> "Server: snooservrn"
[Net::HTTP debug] -> "Vary: Originrn"
[Net::HTTP debug] -> "rn"
[Net::HTTP debug] reading 218756120 bytes...
I went inside with byebug until found where it happens:
[159, 168] in /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/response.rb
159: def reading_body(sock, reqmethodallowbody) #:nodoc: internal use only
160: @socket = sock
161: @body_exist = reqmethodallowbody && self.class.body_permitted?
162: begin
163: yield
=> 164: self.body # ensure to read body
165: ensure
166: @socket = nil
167: end
168: end
(byebug) where
--> #0 Net::HTTPResponse.reading_body(sock#Net::BufferedIO, reqmethodallowbody#TrueClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/response.rb:164
#1 Net::HTTP.transport_request(req#Net::HTTP::Get) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1445
#2 Net::HTTP.request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1407
#3 block in Net::HTTP.block in request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1400
#4 Net::HTTP.start at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:853
#5 Net::HTTP.request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1398
Is it server's fault? Ruby fault? Or should I use some another method if I want to get the header data without loading the whole resource?
P.S.: I do not need third-party fancy dependency, I need to use only the Net::HTTP.
ruby http-headers net-http
add a comment |
I was sure that when I do like this:
http = Net::HTTP.start uri.host, uri.port
request = Net::HTTP::Get.new uri
response = http.request request
I get some sort of established connection to the remote HTTP resource to be able to tell, for example, its Content-Type, while to really load the whole resource I then call the response.body.
But either I was always wrong or it is something with the server I access right now, the http.request loads the whole remote file that is unacceptable for me:
[Net::HTTP debug] opening connection to v.redd.it:80...
[Net::HTTP debug] opened
[Net::HTTP debug] <- "GET /6otzwem1c7721/DASH_9_6_M?source=fallback HTTP/1.1rnAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3rnAccept: */*rnUser-Agent: RubyrnHost: v.redd.itrnConnection: closernrn"
[Net::HTTP debug] -> "HTTP/1.1 200 OKrn"
[Net::HTTP debug] -> "Last-Modified: Sat, 29 Dec 2018 11:25:57 GMTrn"
[Net::HTTP debug] -> "ETag: "662291aec20b252aaebcf54c3b1827af-42"rn"
[Net::HTTP debug] -> "Content-Type: video/mp4rn"
[Net::HTTP debug] -> "Cache-Control: public, max-age=604800, s-maxage=86400, must-revalidatern"
[Net::HTTP debug] -> "Accept-Ranges: bytesrn"
[Net::HTTP debug] -> "Content-Length: 218756120rn"
[Net::HTTP debug] -> "Accept-Ranges: bytesrn"
[Net::HTTP debug] -> "Date: Sun, 30 Dec 2018 13:44:21 GMTrn"
[Net::HTTP debug] -> "Via: 1.1 varnishrn"
[Net::HTTP debug] -> "Connection: closern"
[Net::HTTP debug] -> "X-Served-By: cache-fra19120-FRArn"
[Net::HTTP debug] -> "X-Cache: HITrn"
[Net::HTTP debug] -> "X-Cache-Hits: 0rn"
[Net::HTTP debug] -> "X-Timer: S1546177461.284280,VS0,VE0rn"
[Net::HTTP debug] -> "Server: snooservrn"
[Net::HTTP debug] -> "Vary: Originrn"
[Net::HTTP debug] -> "rn"
[Net::HTTP debug] reading 218756120 bytes...
I went inside with byebug until found where it happens:
[159, 168] in /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/response.rb
159: def reading_body(sock, reqmethodallowbody) #:nodoc: internal use only
160: @socket = sock
161: @body_exist = reqmethodallowbody && self.class.body_permitted?
162: begin
163: yield
=> 164: self.body # ensure to read body
165: ensure
166: @socket = nil
167: end
168: end
(byebug) where
--> #0 Net::HTTPResponse.reading_body(sock#Net::BufferedIO, reqmethodallowbody#TrueClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/response.rb:164
#1 Net::HTTP.transport_request(req#Net::HTTP::Get) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1445
#2 Net::HTTP.request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1407
#3 block in Net::HTTP.block in request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1400
#4 Net::HTTP.start at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:853
#5 Net::HTTP.request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1398
Is it server's fault? Ruby fault? Or should I use some another method if I want to get the header data without loading the whole resource?
P.S.: I do not need third-party fancy dependency, I need to use only the Net::HTTP.
ruby http-headers net-http
1
UseHEADinstead ofGETif you don't care about the body.
– Marcin Kołodziej
Dec 30 '18 at 15:00
GETis supposed to get the whole resource from the server, I am unsure why you expected it to perform several requests to load this, than that, etc. As @MarcinKołodziej says, to get the header people in the internets useHEADrequests explicitly designed for this purpose.
– Aleksei Matiushkin
Dec 30 '18 at 15:03
add a comment |
I was sure that when I do like this:
http = Net::HTTP.start uri.host, uri.port
request = Net::HTTP::Get.new uri
response = http.request request
I get some sort of established connection to the remote HTTP resource to be able to tell, for example, its Content-Type, while to really load the whole resource I then call the response.body.
But either I was always wrong or it is something with the server I access right now, the http.request loads the whole remote file that is unacceptable for me:
[Net::HTTP debug] opening connection to v.redd.it:80...
[Net::HTTP debug] opened
[Net::HTTP debug] <- "GET /6otzwem1c7721/DASH_9_6_M?source=fallback HTTP/1.1rnAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3rnAccept: */*rnUser-Agent: RubyrnHost: v.redd.itrnConnection: closernrn"
[Net::HTTP debug] -> "HTTP/1.1 200 OKrn"
[Net::HTTP debug] -> "Last-Modified: Sat, 29 Dec 2018 11:25:57 GMTrn"
[Net::HTTP debug] -> "ETag: "662291aec20b252aaebcf54c3b1827af-42"rn"
[Net::HTTP debug] -> "Content-Type: video/mp4rn"
[Net::HTTP debug] -> "Cache-Control: public, max-age=604800, s-maxage=86400, must-revalidatern"
[Net::HTTP debug] -> "Accept-Ranges: bytesrn"
[Net::HTTP debug] -> "Content-Length: 218756120rn"
[Net::HTTP debug] -> "Accept-Ranges: bytesrn"
[Net::HTTP debug] -> "Date: Sun, 30 Dec 2018 13:44:21 GMTrn"
[Net::HTTP debug] -> "Via: 1.1 varnishrn"
[Net::HTTP debug] -> "Connection: closern"
[Net::HTTP debug] -> "X-Served-By: cache-fra19120-FRArn"
[Net::HTTP debug] -> "X-Cache: HITrn"
[Net::HTTP debug] -> "X-Cache-Hits: 0rn"
[Net::HTTP debug] -> "X-Timer: S1546177461.284280,VS0,VE0rn"
[Net::HTTP debug] -> "Server: snooservrn"
[Net::HTTP debug] -> "Vary: Originrn"
[Net::HTTP debug] -> "rn"
[Net::HTTP debug] reading 218756120 bytes...
I went inside with byebug until found where it happens:
[159, 168] in /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/response.rb
159: def reading_body(sock, reqmethodallowbody) #:nodoc: internal use only
160: @socket = sock
161: @body_exist = reqmethodallowbody && self.class.body_permitted?
162: begin
163: yield
=> 164: self.body # ensure to read body
165: ensure
166: @socket = nil
167: end
168: end
(byebug) where
--> #0 Net::HTTPResponse.reading_body(sock#Net::BufferedIO, reqmethodallowbody#TrueClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/response.rb:164
#1 Net::HTTP.transport_request(req#Net::HTTP::Get) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1445
#2 Net::HTTP.request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1407
#3 block in Net::HTTP.block in request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1400
#4 Net::HTTP.start at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:853
#5 Net::HTTP.request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1398
Is it server's fault? Ruby fault? Or should I use some another method if I want to get the header data without loading the whole resource?
P.S.: I do not need third-party fancy dependency, I need to use only the Net::HTTP.
ruby http-headers net-http
I was sure that when I do like this:
http = Net::HTTP.start uri.host, uri.port
request = Net::HTTP::Get.new uri
response = http.request request
I get some sort of established connection to the remote HTTP resource to be able to tell, for example, its Content-Type, while to really load the whole resource I then call the response.body.
But either I was always wrong or it is something with the server I access right now, the http.request loads the whole remote file that is unacceptable for me:
[Net::HTTP debug] opening connection to v.redd.it:80...
[Net::HTTP debug] opened
[Net::HTTP debug] <- "GET /6otzwem1c7721/DASH_9_6_M?source=fallback HTTP/1.1rnAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3rnAccept: */*rnUser-Agent: RubyrnHost: v.redd.itrnConnection: closernrn"
[Net::HTTP debug] -> "HTTP/1.1 200 OKrn"
[Net::HTTP debug] -> "Last-Modified: Sat, 29 Dec 2018 11:25:57 GMTrn"
[Net::HTTP debug] -> "ETag: "662291aec20b252aaebcf54c3b1827af-42"rn"
[Net::HTTP debug] -> "Content-Type: video/mp4rn"
[Net::HTTP debug] -> "Cache-Control: public, max-age=604800, s-maxage=86400, must-revalidatern"
[Net::HTTP debug] -> "Accept-Ranges: bytesrn"
[Net::HTTP debug] -> "Content-Length: 218756120rn"
[Net::HTTP debug] -> "Accept-Ranges: bytesrn"
[Net::HTTP debug] -> "Date: Sun, 30 Dec 2018 13:44:21 GMTrn"
[Net::HTTP debug] -> "Via: 1.1 varnishrn"
[Net::HTTP debug] -> "Connection: closern"
[Net::HTTP debug] -> "X-Served-By: cache-fra19120-FRArn"
[Net::HTTP debug] -> "X-Cache: HITrn"
[Net::HTTP debug] -> "X-Cache-Hits: 0rn"
[Net::HTTP debug] -> "X-Timer: S1546177461.284280,VS0,VE0rn"
[Net::HTTP debug] -> "Server: snooservrn"
[Net::HTTP debug] -> "Vary: Originrn"
[Net::HTTP debug] -> "rn"
[Net::HTTP debug] reading 218756120 bytes...
I went inside with byebug until found where it happens:
[159, 168] in /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/response.rb
159: def reading_body(sock, reqmethodallowbody) #:nodoc: internal use only
160: @socket = sock
161: @body_exist = reqmethodallowbody && self.class.body_permitted?
162: begin
163: yield
=> 164: self.body # ensure to read body
165: ensure
166: @socket = nil
167: end
168: end
(byebug) where
--> #0 Net::HTTPResponse.reading_body(sock#Net::BufferedIO, reqmethodallowbody#TrueClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/response.rb:164
#1 Net::HTTP.transport_request(req#Net::HTTP::Get) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1445
#2 Net::HTTP.request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1407
#3 block in Net::HTTP.block in request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1400
#4 Net::HTTP.start at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:853
#5 Net::HTTP.request(req#Net::HTTP::Get, body#NilClass, &block#NilClass) at /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http.rb:1398
Is it server's fault? Ruby fault? Or should I use some another method if I want to get the header data without loading the whole resource?
P.S.: I do not need third-party fancy dependency, I need to use only the Net::HTTP.
ruby http-headers net-http
ruby http-headers net-http
asked Dec 30 '18 at 14:11
NakilonNakilon
26.9k1285106
26.9k1285106
1
UseHEADinstead ofGETif you don't care about the body.
– Marcin Kołodziej
Dec 30 '18 at 15:00
GETis supposed to get the whole resource from the server, I am unsure why you expected it to perform several requests to load this, than that, etc. As @MarcinKołodziej says, to get the header people in the internets useHEADrequests explicitly designed for this purpose.
– Aleksei Matiushkin
Dec 30 '18 at 15:03
add a comment |
1
UseHEADinstead ofGETif you don't care about the body.
– Marcin Kołodziej
Dec 30 '18 at 15:00
GETis supposed to get the whole resource from the server, I am unsure why you expected it to perform several requests to load this, than that, etc. As @MarcinKołodziej says, to get the header people in the internets useHEADrequests explicitly designed for this purpose.
– Aleksei Matiushkin
Dec 30 '18 at 15:03
1
1
Use
HEAD instead of GET if you don't care about the body.– Marcin Kołodziej
Dec 30 '18 at 15:00
Use
HEAD instead of GET if you don't care about the body.– Marcin Kołodziej
Dec 30 '18 at 15:00
GET is supposed to get the whole resource from the server, I am unsure why you expected it to perform several requests to load this, than that, etc. As @MarcinKołodziej says, to get the header people in the internets use HEAD requests explicitly designed for this purpose.– Aleksei Matiushkin
Dec 30 '18 at 15:03
GET is supposed to get the whole resource from the server, I am unsure why you expected it to perform several requests to load this, than that, etc. As @MarcinKołodziej says, to get the header people in the internets use HEAD requests explicitly designed for this purpose.– Aleksei Matiushkin
Dec 30 '18 at 15:03
add a comment |
1 Answer
1
active
oldest
votes
Depending on what you actually want to accomplish:
Don't care about the body at all:
Use HEAD instead of GET:
uri = URI('http://example.com')
http = Net::HTTP.start uri.host, uri.port
request = Net::HTTP::Head.new uri
response = http.request request
response.body
# => nil
Load the body conditionally
Using blocks with net/http will let you hook before the body is actually loaded:
uri = URI('http://example.com')
res = nil
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new uri
http.request request do |response|
res = response
break
end
end
res
# => #<Net::HTTPOK 200 OK readbody=false>
res['Content-Type']
# => "text/html; charset=UTF-8"
Thank you. I'll accept the answer once I manage this to work.
– Nakilon
Dec 30 '18 at 16:34
Unfortunately a lot of web services do not support HEAD. And not all of them respond 405 to show this specific error.
– Nakilon
Jan 20 at 22:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978308%2fhow-do-i-read-the-header-without-reading-the-rest-of-the-http-resource%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Depending on what you actually want to accomplish:
Don't care about the body at all:
Use HEAD instead of GET:
uri = URI('http://example.com')
http = Net::HTTP.start uri.host, uri.port
request = Net::HTTP::Head.new uri
response = http.request request
response.body
# => nil
Load the body conditionally
Using blocks with net/http will let you hook before the body is actually loaded:
uri = URI('http://example.com')
res = nil
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new uri
http.request request do |response|
res = response
break
end
end
res
# => #<Net::HTTPOK 200 OK readbody=false>
res['Content-Type']
# => "text/html; charset=UTF-8"
Thank you. I'll accept the answer once I manage this to work.
– Nakilon
Dec 30 '18 at 16:34
Unfortunately a lot of web services do not support HEAD. And not all of them respond 405 to show this specific error.
– Nakilon
Jan 20 at 22:17
add a comment |
Depending on what you actually want to accomplish:
Don't care about the body at all:
Use HEAD instead of GET:
uri = URI('http://example.com')
http = Net::HTTP.start uri.host, uri.port
request = Net::HTTP::Head.new uri
response = http.request request
response.body
# => nil
Load the body conditionally
Using blocks with net/http will let you hook before the body is actually loaded:
uri = URI('http://example.com')
res = nil
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new uri
http.request request do |response|
res = response
break
end
end
res
# => #<Net::HTTPOK 200 OK readbody=false>
res['Content-Type']
# => "text/html; charset=UTF-8"
Thank you. I'll accept the answer once I manage this to work.
– Nakilon
Dec 30 '18 at 16:34
Unfortunately a lot of web services do not support HEAD. And not all of them respond 405 to show this specific error.
– Nakilon
Jan 20 at 22:17
add a comment |
Depending on what you actually want to accomplish:
Don't care about the body at all:
Use HEAD instead of GET:
uri = URI('http://example.com')
http = Net::HTTP.start uri.host, uri.port
request = Net::HTTP::Head.new uri
response = http.request request
response.body
# => nil
Load the body conditionally
Using blocks with net/http will let you hook before the body is actually loaded:
uri = URI('http://example.com')
res = nil
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new uri
http.request request do |response|
res = response
break
end
end
res
# => #<Net::HTTPOK 200 OK readbody=false>
res['Content-Type']
# => "text/html; charset=UTF-8"
Depending on what you actually want to accomplish:
Don't care about the body at all:
Use HEAD instead of GET:
uri = URI('http://example.com')
http = Net::HTTP.start uri.host, uri.port
request = Net::HTTP::Head.new uri
response = http.request request
response.body
# => nil
Load the body conditionally
Using blocks with net/http will let you hook before the body is actually loaded:
uri = URI('http://example.com')
res = nil
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new uri
http.request request do |response|
res = response
break
end
end
res
# => #<Net::HTTPOK 200 OK readbody=false>
res['Content-Type']
# => "text/html; charset=UTF-8"
answered Dec 30 '18 at 15:16
Marcin KołodziejMarcin Kołodziej
4,3111315
4,3111315
Thank you. I'll accept the answer once I manage this to work.
– Nakilon
Dec 30 '18 at 16:34
Unfortunately a lot of web services do not support HEAD. And not all of them respond 405 to show this specific error.
– Nakilon
Jan 20 at 22:17
add a comment |
Thank you. I'll accept the answer once I manage this to work.
– Nakilon
Dec 30 '18 at 16:34
Unfortunately a lot of web services do not support HEAD. And not all of them respond 405 to show this specific error.
– Nakilon
Jan 20 at 22:17
Thank you. I'll accept the answer once I manage this to work.
– Nakilon
Dec 30 '18 at 16:34
Thank you. I'll accept the answer once I manage this to work.
– Nakilon
Dec 30 '18 at 16:34
Unfortunately a lot of web services do not support HEAD. And not all of them respond 405 to show this specific error.
– Nakilon
Jan 20 at 22:17
Unfortunately a lot of web services do not support HEAD. And not all of them respond 405 to show this specific error.
– Nakilon
Jan 20 at 22:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978308%2fhow-do-i-read-the-header-without-reading-the-rest-of-the-http-resource%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use
HEADinstead ofGETif you don't care about the body.– Marcin Kołodziej
Dec 30 '18 at 15:00
GETis supposed to get the whole resource from the server, I am unsure why you expected it to perform several requests to load this, than that, etc. As @MarcinKołodziej says, to get the header people in the internets useHEADrequests explicitly designed for this purpose.– Aleksei Matiushkin
Dec 30 '18 at 15:03