405 Method not allowed on Net::HTTP request [ruby on rails] - ruby-on-rails

I'm trying to verify if there is a remote url with following code:
endpoint_uri = URI.parse(#endpoint.url)
endpoint_http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
endpoint_request = Net::HTTP::Head.new(endpoint_uri.request_uri)
endpoint_response = endpoint_http.request(endpoint_request)
I'm still getting 405 Method not allowed. When I use Get instead Head in Net::HTTP::Head.new I'm getting 200 Success but also with whole remote document in response what results in bigger response time (0.3s => 0.9s).
Any ideas why this is happening? Thx

There's a chance that the #endpoint url you're trying to interact with doesn't support HEAD requests (which would be really weird, but still may be the case). Your code works fine for me with a handful of urls (google.com, stackoverflow.com, etc.)
Have you tried a curl request to see what it returns?
curl -I http://www.the_website_you_want_to_test.com

Related

HTTP_ORIGIN always returning nil

I'm trying to get the origin of the request cross-site, I found this answer How to get "Origin" request header in Rails which suggest using
request.headers['origin']
I also tried
request.headers['HTTP_ORIGIN']
but both seem to return nil
I'm running this on Rails 6 app
There is nothing wrong with your code.
To get a header in Rails, just access it inside request.headers.
For example, to get User-Agent:
request.headers['User-Agent']
And since rails 5, we have the HTTP_ version also:
request.headers['HTTP_USER_AGENT']
The problem with the Origin header is that it does not always appear in request headers from the browser. It's just needed when the browser sending a cross-origin request.
You can try with some other tools like Postman or curl to see the header is really there:
curl -H "Origin: http://yourserver.com" http://yourserver.com

Requests.get giving wrong status code for my URL

Got some URLs which I'm testing using the requests library. Example code can be found below:
page = requests.get(url)
print (page.status_code)
#output: 200
Some URLs returns a 404 status code when tested manually. Why is the output giving 200 then?
The programmer can define the status code manually. And it may have misled you.
the HTTP status code “200”, means is “file found.” if you redirect to another page, you will get 200 status code
So You do not let your request redirects.
r = requests.get('http://example.com/sdfsdfs', allow_redirects=False)
print(r.status_code)

Rails net:http PUT request - curl - Tika server

I have a remote TIKA server set up and I'm trying to use it from within a RoR application. I need to pull a file from a remote location and send it on to the Tika server. The wiki for TikaJAXRS gives an example using curl, but I have not been able to get that to work. What does work is this:
curl https://mydomain.s3.amazonaws.com/uploads/testdocument.docx | curl -v -i -X PUT -T - ec2...154.uswest2.compute.amazonaws.com:9998/tika
How do I render this in my Rails app using net::http? I've successfully written a GET request with net::http to the Tika server from the Rails app and gotten back the expected result, but the documentation on PUT is a bit sparse. (The server does require a PUT rather than POST.)
BTW, if anyone knows how to make that last example in that wiki work and render it in net::http, that would be even better!
Addendum:
Here's what I have in the RoR app that doesn't work:
ENDPOINT = "http://ec2...154.us-west-2.compute.amazonaws.com:9998"
file = "https://mydomain.s3.amazonaws.com/uploads/testdocument.docx"
uri = URI.parse(endpoint)
#http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Put.new("/tika")
request.body = URI.parse(file).read
#response = #http.request(request)
and I get back a code 415
I need to know how to change this code to do what the curl commands (curl remote_file piped to curl PUT) are doing successfully.
Update
After a couple of days of fruitless attempts on this, I have a workaround:
gem 'curb'
#response = Curl.put("http://ec2...154.us-west-2.compute.amazonaws.com:9998/tika",
Curl.get("https://mydomain.s3.amazonaws.com/uploads/testdocument.docx").body_str)
While this does provide a solution to my immediate problem, I still want to know how to implement this same functionality more directly by using Net::HTTP.

Checking HEAD from NSMutableURLRequest without Error

I'm working on a program for the iPhone and I ran into a bit of a snafu. I want to load a URL into a WebView, however, I want to make sure that the URL actually exists and is working before trying to load it into the view.
Imagine you have 200 servers and you know your file exists you just don't know which server. It is at http://serverXXX.mydomain.com/myfile.html where XXX is the server # 0 through 200
I wrote a bash script that uses cURL to loop through the servers checking the HEAD request of each iteration of the url and timing out after 1 second:
http://server1.mydomain.com/myfile.html
http://server2.mydomain.com/myfile.html
...
http://server199.mydomain.com/myfile.html
http://server200.mydomain.com/myfile.html
When it gets back a response it greps the response and compares it. If the response is OK it loads the URL in an external program and exits the script. How do I do this in Objective-C without the error "The requested URL was not found on this server." popping up?? I don't want to click OK 200 times while it tries to find the correct URL.
You can do the same as your script with an instance of NSMutableURLRequest and a NSURLConnection. You can set the request to HEAD (setHTTPMethod:) and iterate over the server URLs updating the request after each response (connection:didReceiveResponse:) is received.

Adobe Flex 3 : Fault Event doesnt return XML Feed sent from Server

I am working on a flex application which communicates with a Rails backened.
When i request for some data, It sends back xml feed.
In some cases, if given parameters are not valid, then rails return an error feed with status code = 422 as following
email is wrong
But I dont get this feed in FaultEvent of Flex, How could i read error feed?
Thanks
Are you getting the result in ResultEvent in such cases? I am not sure for what all HTTP error codes FaultEvent will get invoke(I know only it goes for 404 and 500). May be its still going to ResultEvent as a valid result!
You can use HTTPService instead of URLLoader.
Flex HTTP results will not include the actual underlying HTTP response codes. It just doesn't work. (TM)

Resources