Display incoming xml request in Rails - ruby-on-rails

Hey guys,
Is there an easy way to display the complete xml request I'm getting posted to one of my methods?
Thanks a lot

If you want to see the actual XML that is being posted rather than a params hash try
xml_data = request.raw_post
Then to get a hash out of that you could do
hash_from_xml_data = Hash.from_xml(xml_data)

I'm inferring a bit, but you probably want request.body.
An easy, though slightly dirty, way to view it is to raise it as an error in your controller: raise request.body

Related

How to get query string in controller from a GET request minus one param

My Rails app receives a GET request with the following params
https://example.com/auth?code=ABCD&hmac=EFGH&host=IJKL
I need to get the query string without the hmac param so it looks like this code=ABCD&host=IJKL
Can someone help me get started?
Doesn't feel very elegant but I got it to work with this
params.except(:hmac, :controller, :action).to_unsafe_hash.to_query
or
Rack::Utils.build_query(request.query_parameters.except("hmac"))
Hopefully someone has a cleaner solution

Ruby rest client array of params

I'm updating my rest-client gem from 1.8 to 2.0.
On 1.8 it sends an array of params on a get request as my-url?ids=1,2,3,4.
But on 2.0 it uses duplicated keys like my-url?ids=1&ids=2&ids=3. For reasons beyond the context of this question, our back end does NOT support the new multiple keys syntax (ok, it supports it, but we'll have to make a big refactor). So I'd like to know if there's a way to use the 2.0 client version and keep sending get array requests with only one key and separated by comma as before?
Based on the rest-client docs https://github.com/rest-client/rest-client#query-parameters it seems your only option would be to serialize the parameters yourself and add them to the URL as the query string.
If you don't like this behavior and want more control, just serialize params yourself (e.g. with URI.encode_www_form) and add the query string to the URL directly for GET parameters or pass the payload as a string for POST requests.
If you provide some sample code on how you're using the gem, we could help out a bit better with sample answers.
Ok yeah Leo Correa was right, so what I had to do is replace my old code
params = {
partner_key: #partner,
resources: ["front_end_config", "gui_settings"]
}
#response = JSON.parse( RestClient.get( "#{api_base_uri}/partner_config.json", params: params.merge({multipart:true}) ) )
with this new one, serializing and encoding by myself...
params = {
partner_key: #partner,
resources: '["front_end_config", "gui_settings"]'
}
params = URI.encode_www_form(params.merge({multipart:true}))
#response = JSON.parse( RestClient.get( "#{api_base_uri}/partner_config.json?#{params}" ) )
It's ugly as hell, but it worked for me. If there's some other idea on how to make it better, I'd appreciate.

How to pass large data set between controllers in Rails

Currently passing retrieved data using redirect_to which uses GET. Since in some cases data set is large, URI is too long and throws Bad Request error.
All online research says its a bad idea to pass data in GET body. Is there a better way to pass data to another controller?
Code Block
def create
response_body = http_get('/data/I/want')
parsed_result = JSON.parse(response_body)
check_response(parsed_result)
redirect_to controller: :search_results, action: :index, results: parsed_result
end
end point called in create is search results so need to check if results are empty before redirecting and passing the data. I omitted this part from the code block
Any reason to put these code in create method? From my point of view, your code doesn't really create anything. It is just get some JSON data from a remote URL and redirect to search_results#index. Why not just load the JSON in search_results#index directly?
Update
It is too generous to use more than 3 routes for a search action. I have two suggestions:
Search in remote JSON if you can control it. Just pass the searching keyword in URL and let remote resolve it.
If you cannot control the remote JSON, do the search with AJAX call. In your search_results#index, makes an AJAX call to your something like search#new JSON route and fetch the filtered result.

Parsing JSON feed with Ruby on Rails

I know this has been covered over and over but I just can't understand how to get started. Am too much of a JSON virgin to know what's going on.
I'm trying to get some data from a json feed on a remote server into a rails3 application.
I understand I need the json gem but I have no idea how to pull the data.
If the url is something like this:
http://my-feed.com/json?res=service&s=table&table=User&start=0&max=5&sort=id&desc=true
How do I go about getting that from my application?
Any help, walk-throughs, tutorials appreciated
You are going to need to make an HTTP request for the resource, grab the response, and (potentially) parse it yourself - although I think HTTParty will do the JSON parsing for you. HTTParty would be my suggestion for making requests, though.
If you read the README, it links to examples of how to use HTTParty which you should find helpful. The basic example includes how to make a get request, inspect the headers and body, etc. I believe you can also use response.parsed_response to get the parsed response of whatever HTTParty's default parser is. There is also an example of using your own custom parsers (if you prefer another to HTTParty's).
I don't really know the json gem - does it only handle the parsing?
If yes, and you only need to pull in the information, try this:
require 'open-uri'
begin
json = open 'http://my-feed.com/json?res=service&s=table&table=User&start=0&max=5&sort=id&desc=true'
rescue
# a multitude of HTTP-related errors can occur
end
json_string = json.read
# parse

RoR: POST to a page using raw form data. How?

Is there a ruby method to POST form data encoded in "x-www-form-urlencoded" as specified here? http://www.w3.org/MarkUp/html-spec/html-spec_8.html
I am aware of Net::HTTP.post_form, but because I have several values to post which share the same name I can't use a hash, which is required by that method.
To clarify, I have a string of the form "value1=x&value1=y&value1=z&value2=a&value3=b" and I want to be able to POST it to another page. How can I do this?
I think internally the params object is a parsed version of the actual raw post body in the http request. All post data is posted the same way (as raw post data), but the params hash in ActionController has already parsed this into an easy-to-use hash. If you actually need the raw post data from a form, you can access it through the raw_post method of the request object itself.
The ActionController::Request.raw_post documentation here is for rails3, but has been available since at least 2.3.8 (the only 2.3.x version I checked). I think it most likely has been available longer than that.
In a controller, try self.request.raw_post to get the raw post data as a string.
Are you able to have a hash value which is an Array? I think that this is the way parameters with the same names are usually handled.

Resources