How can I Get the RAW URL from a request in Rails? - ruby-on-rails

I'm having trouble debugging a client, and I'm trying to get the raw URL on the server which is in rails. I'm wondering how I can dump the raw URL/http message that is hitting rails.
If found query_string, which works okay for gets. But if a user does a post, I can't seem to find the raw string anywhere.
All I can find is post-parsed parameters in hashes vs raw URLs.
Help?

request.url
Will give the current url.
See other answers:
How do I get the current absolute URL in Ruby on Rails?

Related

Ruby on Rails basic use of RiotGames API (need explanation, solution already found)

First you must know I'm a total beginner, I'm trying to learn so I almost don't know anything.
On the basic page of the API, there is a curl command used as an example to show us how to make requests.
I'm using Ruby on Rails so I used "curl-to-ruby" website to translate it, but it did not work as expected.
I wanted it to show me this :
uri = URI.parse("REQUEST_URL")
response = JSON.parse(Net::HTTP.get(uri))
Instead I got this :
uri = URI.parse("REQUEST_URL")
response = Net:HTTP.get_response(uri)
I don't understand any of this, I thought I wouldn't need to and just use "curl-to-ruby", but apparently I really need to get this.
Would you please try to explain me ?
Or give me links ?
Or matters to read (curl, API, http) ?
Thank you very much, have a nice day.
It's because that command doesn't return just the content, it returns the whole HTTP response object including headers and body. You need to extract the response body and parse that using JSON.parse(), e.g.
JSON.parse(response.body)
See documentation here: https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#method-c-get_response
(Also, there is nothing in the cURL command which would hint to the converter that the content-type of the response was expected to be JSON (e.g. perhaps an "accepts" header or something), so even if it were able to produce extra code adding the JSON.parse part, it has no way of knowing that it would be appropriate to do so in this case.)

Pass json with special characters angularjs to ruby

I need to make a search form, where the back end used is ruby and front end is angular. the search query is generated in angular in json format and is passed to ruby via restangular service.
Its working fine. But when we tested the search string with semicolon it returned 500 Internal error. The first line puts params[:search] gives {"content":"my search is for the actual json string {"content":"my search is ; and :", "options":[]}
Please help me on how to handle it ";" also please let me know what all characters I need to be handled.
You need to encode your params first.
So the encoded params will be:
{"content":"my search is %3B and :", "options":[]}
The following is also valid:
%7B%22content%22%3A%22my+search+is+%3B+and+%3A%22%2C+%22options%22%3A%5B%5D%7D
You can encode URL using: https://www.w3schools.com/tags/ref_urlencode.asp
I am sure Angular will must have some library to encode the URLs.
You could also use native Javascript URL encoder.
The similar question was already answered on SO.

How to use url encoded data in get method?

I am working on a restful service in asp.net MVC 4 Web API. I have a get method that getting a url encoded data as a parameter from URI. But, the thing is when I try this method from RESTClient (firefox tool), I am getting 404. And when i check the stack trace my url seems with non encoded parameter. You can see the example below:
Url format: /api/Applications/key
I am sending this:
/api/Applications/3baxYhYzpVGTrIIzs4CvGv4KIcIRHn5yqx%2F9a5PzTJxK4SWVcwi9GI6bib9pe1psk%2FdYeD0EaOdMHZpzWl%2Fbwg%3D%3D
In the response body, request url seems like this;
/api/Applications/3baxYhYzpVGTrIIzs4CvGv4KIcIRHn5yqx/9a5PzTJxK4SWVcwi9GI6bib9pe1psk/dYeD0EaOdMHZpzWl/bwg==
I am getting an error because of escape characters. I can use post but I want to go on with restful architecture.
Any advice will be appreciated.

How to get rails to parse a url with a hashbang?

I have an incoming url that looks like this (due to my AngularJS configuration):
/new_query#?query=somequery
If the hashbang (#) hadn't been there, Rails would parse it correctly, and extract the query parameter into the params variable. However, it doesn't seem to successfully extract the params when the hashbang is there.
Is there any standard way / config to solve this?
Or would I have to monkey patch some parameter parsing code in rails, by i.e. making a regular expression to find the # and remove it from the url string?

Do I need to encode strings (eg URL) I pass as a POST form parameter

Do I need to encode strings (eg URL) I pass as a POST form parameter?
Ie I want to pass a URL I have to my web application (ruby on rails) as one of the form parameters. So are there any potential characters in a URL/URI that would need to be encoded? Or perhaps rails would handle this anyway?
Do I need to encode strings (eg URL) I pass as a POST form parameter?
That depends on what you're using to create/send your POST request. If you're directly creating the request body yourself, then yes you would have to URL-encode each parameter:
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
foo=bar&url=http://www.example.com/url?innerparameter1=1&innerparameter2=2
this is no good:innerparameter2 is actually a parameter of the outer form-encoded string. It would need encoding, which would look like:
foo=bar&url=http%3A//www.example.com/url%3Finnerparameter1%3D1%26innerparameter2%3D2
If, however, you are using something higher-level to make the POST request, and passing in some kind of mapping of parameter strings, I would expect that component to take care of the URL-encoding for you.
Code?
As bobince mentions, you need to encode any data that you're passing as URL parameters. Often whatever library you're using will take care of this. This applies to all HTTP requests BTW.
For example, an API has an endpoint GET /sites/:name.
Using cURL it should look like
curl http://example.com/sites/google%2Ecom
In Ruby/Rails, you can use URI.encode and URI.decode:
>> URI.encode('google.com', /\W/)
"google%2Ecom"
>> URI.decode('google%2Ecom')
"google.com"
As a general statement, if you emit programmatic or user input data to an HTML page, you should encode it for HTML. Bear in mind that URLs often have the & character and that should be encoded, even if browsers appear to handle it okay.
I'm not a Ruby guy, so I don't know how you do that in Ruby, nor am I familiar with Ruby on Rails to say if it will do it (though I would be a little surprised by that), but the guideline I suggest isn't language specific.

Resources