net-http

Ruby Proxy Authentication GET/POST with OpenURI or net/http

吃可爱长大的小学妹 提交于 2019-12-22 05:51:07
问题 I'm using ruby 1.9.3 and trying to use open-uri to get a url and try posting using Net:HTTP Im trying to use proxy authentication for both: Trying to do a POST request with net/http : require 'net/http' require 'open-uri' http = Net::HTTP.new("google.com", 80) headers = { 'User-Agent' => 'Ruby 193'} resp, data = http.post("/", "name1=value1&name2=value2", headers) puts data And for open-uri which I can't get to do POST I use: data = open("http://google.com/","User-Agent"=> "Ruby 193").read

TypeError: can't convert Net::HTTPOK into String

瘦欲@ 提交于 2019-12-21 11:54:49
问题 I'm using net/http and json to geocode an address using google's Geocoding API. Here is where the error is being thrown: response = Net::HTTP.get_response(URI.parse(url)) result = JSON.parse(response) The response is of class Net::HTTPOK, but I want to access the actual JSON response data (not just the status code). 回答1: You want result = JSON.parse(response.body) http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTPResponse.html 来源: https://stackoverflow.com/questions/9353227

How to set a custom user agent in ruby

心已入冬 提交于 2019-12-18 12:51:09
问题 I've a task to test different user agents on a URL through automation. I'm using ruby to code, and I've been trying to set an user agent using the following method, but it doesn't seem to recognize the user agent. @http = Net::HTTP.new(URL) response = @http.request_get(URL, {'User-Agent' => useragent}) Is there any other way to do this, or what am I doing wrong? 回答1: http = Net::HTTP.new("your.site.com", 80) req = Net::HTTP::Get.new("/path/to/the/page.html", {'User-Agent' => 'your_agent'})

Ruby - net/http - following redirects

六月ゝ 毕业季﹏ 提交于 2019-12-17 06:33:55
问题 I've got a URL and I'm using HTTP GET to pass a query along to a page. What happens with the most recent flavor (in net/http ) is that the script doesn't go beyond the 302 response. I've tried several different solutions; HTTPClient, net/http, Rest-Client, Patron... I need a way to continue to the final page in order to validate an attribute tag on that pages html. The redirection is due to a mobile user agent hitting a page that redirects to a mobile view, hence the mobile user agent in the

Process Management for the Go Webserver

倖福魔咒の 提交于 2019-12-17 05:11:53
问题 I'm a new Go programmer, coming from the world of web application and service development. Apologies is this is a herp de-derp question, but my googling around for an answer hasn't found anything. Also, this is borderline Server Fault territory, but since I'm more interested in the APIs/programmatic interfaces I'm asking here. I've written a small go program using the net/http package's built-in web server. I'm getting ready to deploy to production, but I'm a little unclear on the process of

Making HEAD request in Ruby

杀马特。学长 韩版系。学妹 提交于 2019-12-13 14:09:51
问题 I am kind of new to ruby and from a python background I want to make a head request to a URL and check some information like if the file exists on the server and timestamp, etag etc.,I am not able to get this done in RUBY. In Python: import httplib2 print httplib2.Http().request('url.com/file.xml','HEAD') In Ruby: I tried this and throwing some error require 'net/http' Net::HTTP.start('url.com'){|http| response = http.head('/file.xml') } puts response SocketError: getaddrinfo: nodename nor

Ruby OAuth2.0: client credential type has unsupported client authentication method

旧时模样 提交于 2019-12-13 06:26:31
问题 I am using OAuth2 gem, for making a client_credential authentication. My code is as below, require 'oauth2' client = OAuth2::Client.new("my_client_id", "my_client_secret", :site => "my_site_url", :token_url => "oauth2/token") client.client_credentials.get_token When I execute above code block, it respond with below error, OAuth2::Error (invalid_client: Client authentication failed (e.g., unknown client, no client authentication included, or unsupported authentication method)) { "error":

Alllow for “bad” urls in in Rails

流过昼夜 提交于 2019-12-13 06:05:28
问题 I have a simple script which checks for bad url's: def self.check_prod_links require 'net/http' results = [] Product.find_each(:conditions =>{:published => 1}) do |product| url = product.url id = product.id uri = URI(url) begin response = Net::HTTP.get_response(uri) rescue begin http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) rescue begin response = Net:

Posting Ruby data in JSON format with Net/http

99封情书 提交于 2019-12-12 09:03:33
问题 I have this ruby file: require 'net/http' require 'json' require 'uri' #test data newAcctJson ='{ "type": "Credit Card", "nickname": "MoreTesting", "rewards": 2, "balance": 50 }' #creates a new account def createAcct(custID, json) url = "http://api.reimaginebanking.com:80/customers/#{custID}/accounts?key=#{APIkey}" uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) myHash = JSON.parse(json) resp = Net::HTTP.post_form(uri, myHash) puts(resp.body) end which attempts to create a new

Rails - sending multiple http requests to a another server

不问归期 提交于 2019-12-12 03:28:29
问题 1000 users make a request to my Rails server at exactly the same time. For each user, I need to make 50 http requests to other servers, each taking 1 second to respond. How do I go about coding my application so that I can respond to the users as quickly as possible? (Note: the app hasn't launched and the numbers are hypothetical.) 回答1: I think you need to implement a Job queuing system with ActiveJob and for example Sidekiq. So your workers can make these 50 http requests. You'll have to