I'm trying to pass a URL as a param to my Rails app:
Started DELETE "/images/0?s3_filepath=https://s3.amazonaws.com/buildinprogresstest/uploads/blllbyq5k3qinl4l/uploads_2F9qggxxf5dvlsor-667601c8f38d8d41af07828accbf3147_2F2014-07-13%252B18.44.29.jpg" for 127.0.0.1 at 2016-08-31 11:52:09 -0400
The s3_filepath is not being properly parsed in the params:
Parameters: {"s3_filepath"=>"https://s3.amazonaws.com/buildinprogresstest/uploads/blllbyq5k3qinl4l/uploads_2F9qggxxf5dvlsor-667601c8f38d8d41af07828accbf3147_2F2014-07-13%2B18.44.29.jpg", "id"=>"0"}
If you look closely, the filename includes the sequence "252B18" but the params seems to remove the numbers "52"
I'm at a loss as to why this is happening. Any ideas?
Normally parameters are url-encoded and decoded on rails side. %25 is decoded to %, that's why it is removed from your input. You need to properly encode this url.
In Ruby you can use CGI.escape
CGI.escape "https://s3.amazonaws.com/buildinprogresstest/uploads/blllbyq5k3qinl4l/uploads_2F9qggxxf5dvlsor-667601c8f38d8d41af07828accbf3147_2F2014-07-13%252B18.44.29.jpg"
=> "https%3A%2F%2Fs3.amazonaws.com%2Fbuildinprogresstest%2Fuploads%2Fblllbyq5k3qinl4l%2Fuploads_2F9qggxxf5dvlsor-667601c8f38d8d41af07828accbf3147_2F2014-07-13%25252B18.44.29.jpg"
If you send this request via javascript you can use escape function in javascript
escape("https%3A%2F%2Fs3.amazonaws.com%2Fbuildinprogresstest%2Fuploads%2Fblllbyq5k3qinl4l%2Fuploads_2F9qggxxf5dvlsor-667601c8f38d8d41af07828accbf3147_2F2014-07-13%25252B18.44.29.jpg")
Related
I need to include JSON data in the body of a post request. I will hash the data with a pre-defined key and include it in the header. How can I convert JSON to a one line string with no spaces and include it in the body of a post request as raw data?
To remove spaces and newlines
s = old_s.gsub /[ \n]/, ''
I'm not sure why you would need to do this though. It would help if you explained what you are actually trying to accomplish.
Standard Rails' #to_json method will give you JSON without spaces. So you just need to include it in your request:
uri = URI('http://www.example.com/search.cgi')
res = Net::HTTP.post_form(uri, 'myparam' => my_object.to_json)
puts res.body
(Modified example from Net::HTTP documentation)
Any HTTP library (Net::HTTP, HTTParty, etc.) will do all the escaping for you.
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?
Hitting a bit of a brick wall here. I'm trying to send a string containing line breaks (\n, turned to \u000a by JSON.stringify) as part of a JSON object over to a Rails app:
{"bob":{"id":46,"notes":"foo\u000abar\u000abaz"}}
This goes over the wire as this, with \u000a escaped as %5Cu000a:
http://localhost/bobs/46?draft=true&%7B%22bob%22%3A%7B%22id%22%3A46%2C%22notes%22%3A%22foo%5Cu000abar%5Cu000abaz%22%7D%7D=
But the second the request hits Rubyland, the newlines disappear in a puff of ether, turning into spaces:
Processing Api::BobsController#update (for 127.0.0.1 at 2011-05-19 11:01:43) [PUT]
Parameters: {"draft"=>"true", "action"=>"update", "id"=>"46", "controller"=>"api/bobs", "bob"=>{"notes"=>"foo bar baz", "id"=>46}
And it's not just some logging artifact, but they're going into the database that way as well:
ree-1.8.7-2010.02 > Bob.find_by_id(46)
=> #<Bob id: 46, notes: "foo bar baz"...>
If I send eg. "\\n" instead of "\n", they come through fine:
Processing Api::BobsController#update (for 127.0.0.1 at 2011-05-19 11:01:43) [PUT]
Parameters: {"draft"=>"true", "action"=>"update", "id"=>"46", "controller"=>"api/bobs", "bob"=>{"notes"=>"foo\\nbar\\nbaz", "id"=>46}
What's going on, and why?
Update: A colleague vaguely recalls hearing that Passenger has been suspected of dropping some special chars, but he can't find a reference to back this up, and neither can I...?
This could be nothing, but aren't PUT methods meant to be POST'ed in RESTful Rails? GET-ing any URL should be repeatable w/o any change to the database.
If you changed your AJAX call to post you could also indicate proper content-type of application/json so Rails knows how to handle it.
Our app provides an API that people can use to submit URLs like this:
curl -X POST http://app.local/resource -d'url=http://news.google.com/newshl=en&q=obama&um=1&ie=UTF-8&output=rss'
Unfortunately, it seems that Rails messes up with this param. Any idea on how to fix this?
See the log below :
Processing ApplicationController#index (for 127.0.0.1 at 2010-06-08 19:03:09) [POST]
Parameters: {"um"=>"1", "url"=>"http://news.google.com/newshl=en", "output"=>"rss", "q"=>"obama", "ie"=>"UTF-8"}
I would expect the following :
Parameters: {"url"=>"hhttp://news.google.com/newshl=en&q=obama&um=1&ie=UTF-8&output=rss"}
What exactly Rails messes up?
If you are referring to the fact that it didn't get complete Google URL (i.e. separated it to output, q and other params) that's because you need to encode '&' character if you want to use it as a part of a value. Something like:
curl -X POST http://app.local/resource -d'url=http://news.google.com/newshl=en%26q=obama%26um=1%26ie=UTF-8%26output=rss'
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.