Request only the header in http call - ruby

Is there a ruby library, with which I can request the web server to return only the header response and no content? This will help me speed up a script in which all I care is the response code.
I am now using this
Net::HTTP.get_response(URI(url))
but the server generates all the assert files and so on, which I do not want.

You might use rest-client gem and in particular head method.
http://www.rubydoc.info/github/rest-client/rest-client/RestClient#head-class_method

This can be done using 'Net::HTTP::Head' in the net-http library. other libraries also support it, just remember to look for the HEAD method instead of the Get method

Related

Ruby Net::HTTP::Options does not allow response body

I am writing a script to test various web-services in ruby. To make http requests thus far I have been using Net::HTTP but today I realized I needed to make an OPTIONS request and retrieve some JSON from the response.
Unfortunately ruby does not currently support this: https://bugs.ruby-lang.org/issues/8429
Does anyone know of gem that supports this or some other way to get this response?
This is an alternative which supports lot of options
https://rubygems.org/gems/curb
Mechanize Gem
Try this gem it's very usefull and simple in use. I use it for parsing and another different tasks.

vcr with capybara-webkit

I'm using capybara-webkit to test integration with a third party website (I need javascript).
I want to use vcr to record requests made during the integration test but capybara-webkit doesn't go over net http so vcr is unable to record them. How would I go about writing an adaptor for vcr that would allow me to record the reqeusts?
Unfortunately, VCR is very much incompatible with capybara-webkit. The fact is that capybara webkit is using webkit, which is in c. Webmock and Fakeweb, which are the basis for VCR, can only be used for Ruby web requests. Making the two work together would likely be a monumental task.
I've solved this problem two ways:
The first (hacky, but valid) is to add a new javascript file to the application that is only included in the test environment. This file stubs out the JS classes which make external web requests. Aside from the pure hackatude of this approach, it requires that every time a new request is added or changed you must change the stubs as well.
The second approach is to route all external requests through my own server, effectively proxying all external requests through my server. This has the huge disadvantage that you have to have an action for everything you want to consume (you could genericize it, with some work). It also suffers from the fact that it could as much as double the time for the request to complete. However, since the requests are now being made by Ruby you can use VCR in all it's glory.
In my situations, approach #2 has been much more to my advantage thanks to the fact that I need ruby to manipulate the data so that I can keep my javascript source-agnostic. I was, however, using approach #1 for quite a while successfully.
I've written a small ruby library (puffing-billy) for rspec+capybara that does exactly this -- it injects a proxy in between your browser and the outside world and allows you to fake responses to specific requests.
Example:
describe 'fetching badges from stackoverflow API' do
it 'should show a nice message when you have no badges' do
# stub some JSONP
proxy.stub('http://api.stackoverflow.com/1.1/users/1/badges',
:jsonp => { :badges => [] })
visit '/my_badges'
page.should have_content("You don't have any badges :(")
end
end

How to view GET request headers generated by open-uri

I'm using open-uri and I know I can get the response headers with the meta method as below, but I'm wondering how I can view the GET headers generated. Or would I have to use a different library for this?
page = open('http://www.google.com');
page.meta
Thanks for the help
You can set the request parameters with open('http://example.com', 'User-Agent' => 'Me'), but to see all the headers that will be sent you will probably have to sniff the traffic with a proxy, ngrep, or tcpflow. Or read the source.
open-uri is a convenience library for quickly fetching and reading files over a network. If that's not your use case, you can use a lower-level library like Net::HTTP to get more control over the connection.

any gem available that will pull out the get and post parameters ofsource code that I've got using net/http?

I am pulling the source code of a webpage using net/http and I was wondering if there was anyway to parse out the get and post parameters so that they can be listed?
Thanks,
Tom
GET and POST parameters are in the HTTP header, not the HTML source. So the answer is no, you can't get it from the source, unless you know that the information has been somehow encoded in HTML, or can do that yourself.
However, any GET or POST parameters would have been sent by your Net::HTTP code, so you can print those out yourself.

Ruby Oauth File upload/Multipart POST request

I've been looking at this for a couple of days now and haven't
found a solution. Is there a way to upload a file using OAuth-Ruby?
I am working with a REST system that protects their resource with oauth. I am building a test tool using ruby and oauth-ruby to make it easier to upload test data to the system. But I can't get around to upload files to the resources.
When I send a normal request, everything works but adding a file as a
parameter makes the signature invalid.
Example:
#access_token.post("http://.../imageresource", {:name=>"awesome cat"}, {'Content-Type' => 'multipart/form-data'})
works but gives me:
<error>
<message>images/POST: Request has no file data</message>
</error>
I am not sure how to add a file to the post.
Any thoughts on this?
Thanks,
I know this is old but I'm looking to do this too, this looks like it could do the trick.
Actually there's a question ruby-how-to-post-a-file-via-http-as-multipart-form-data that has an example.
This is either impossible to do with the oauth gem or exceedingly difficult. Either way, I don't know of any way to do it using that gem.
It can be done trivially with my signet gem as long as you have a handy way to construct a valid multipart request body. The construction of such a request body is out-of-scope of an OAuth gem, but should be pretty easy to do with most HTTP clients. The httpadapter gem can then translate the request into a form that signet can sign. Let me know if your preferred HTTP client isn't supported by httpadapter and I'll get that resolved immediately.
See the second example on the fetch_protected_resource method to get an idea for how this might be done.

Resources