Pass json with special characters angularjs to ruby - ruby-on-rails

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.

Related

Bad URI on heroku when using HTTParty

I currently am getting an the following error when using httparty on heroku.
HTTParty works for the first 11 times but on the 12th time it shows this error.
I am trying to seed data into my database.
When I go to the URL via my browser, it works. I ran the same code in development and it works. I am unsure what to do. Please help!
You pass invalid URI -
"https://maps.googlemaps.com/maps/api/geocode/json?address=San Fransisco"
address has space in URI.
So, wherever you pass params, Do -
uri_path = https://maps.googlemaps.com/maps/api/geocode/json
params = {address: "San Fransisco",...............}
"#{uri_path}?#{params.to_param}"
Amit Suroliya was correct. I was using HTTParty as well and it was working through Curl and Development but would crash in production. Because the HTTParty parameter is the literal URL (as a string), it has to be a flawless URL/URI (meaning no spaces). My bad URI was as follows:
HTTParty.get("http://api.blahblahblah.com/v1/Boards/Search?&limit=79&query=#{query}&filter_date_from=1423353600")
Notice the interpolation query=#{query}"
So if query='Michelle Obama', notice space between Michelle and Obama. Because of interpolation, the HTTParty.get('string') it is incorrect.
Solution:
Replace all whitespaces within your string with +, or you could use %20.
I used query.gsub!(' ', '+')
For more info on whitespace in the URL check it out here: Spaces in URLs?

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?

How send parameters with decimal point in url?

I’m using Backbone.js and Rails.
In Backbone.js I use HTML5 push state to set filter parameters in a url.
When the page is reloading I want to pass these parameters to Rails.
I encoded a parameter lat:34.34+lng:45.23 using JavaScript’s encodeURIComponent. It encoded:
/users/nearby/lat:34.34+lng:45.23/
as:
/users/nearby/lat%3A34.34%2Blng%3A45.23
but this route is not found.
If I delete the points from url, it works.
How can I send parameters with a decimal point?
The . is not a character that has to be encoded. Is this causing issues server side?
See here for more details:
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
I solved my problems adding an "extra" slash to the end of the url.
In your encoded url it is missing.
Hope this helps
One manifestation of this problem is in URL pattern matching, where the Query Param is expected (i.e. matched) to be an integer. This does not match a number with a decimal point. So you get a 404 (URL not found).

How can I Get the RAW URL from a request in 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?

What is the proper way to sanitize user input when using a Ruby system call?

I have a Ruby on Rails Application that is using the X virtual framebuffer along with another program to grab images from the web. I have structured my command as shown below:
xvfb-run --server-args=-screen 0 1024x768x24 /my/c++/app #{user_provided_url}
What is the best way to make this call in rails with the maximum amount of safety from user input?
You probably don't need to sanitize this input in rails. If it's a URL and it's in a string format then it already has properly escaped characters to be passed as a URL to a Net::HTTP call. That said, you could write a regular expression to check that the URL looks valid. You could also do the following to make sure that the URL is parse-able:
uri = URI.parse(user_provided_url)
You can then query the object for it's relevant parts:
uri.path
uri.host
uri.port
Maybe I'm wrong, but why don't you just make sure that the string given is really an URL (URI::parse), surround it with single quotes and escape any single quote (') character that appears inside?

Resources