Configure Ember to send JSON to Rails Server - ruby-on-rails

I want to send base64 image string to my server. When I send it as a parameter in my controller like this:
this.get('store').createRecord(Emb.Painting, {name: newName, image:base64string]});
this.get('store').commit();
The upload succeeds but I get RequestURITooLarge error.
How do I get ember to send my data as a JSON package?
If anyone could also explain how to recieve JSON in the rails controller thatd be great too.

Related

Rails-API How to test Active Storage

I added Active Storage into my Rails API application and want to test it, but I don't know how to do it. I was trying send file with JSON data in Postman, but JSON data doesn't send correctly or I am doing something wrong. I did it like that:
Image from postman
Is there any option to send request with file and JSON data without creating any view?
As far as I know, an upload can't be done via an API rest way. You need to use enctype: multipart/form-data as regular POST done via form.
In your screenshot you are already sending as form-post, because you chose to send via form-data. This is why your json isn't properly parsed by rails.
If you want to upload an image and post data in the same request you will need to break your json attributes into form data fields like:
data[name]=asaa
data[last_name]=foo
...
Or, you can send a JSON in your data field and do the manual parsing when fetching in controller like:
def upload
file = params[:file]
data = parsed_params_data!(params)
# do your magic
end
def parsed_params_data!(params)
JSON.parse(params[:data])
end

How to make rails API able to accept json params instead of http

Generic rails API use jbuilder for rendering result.
The http params used to send data to rails API server, like this:
POST user_id=10&post[0][msg]=text&post[1][post_id]=15&post[1][msg]=message
How to make API able to accept json inbound params instead of http, like this:
POST {user_id:10,post:[{msg:'text'},{post_id:15,msg:'message'}]}
According to the comment by #Julia Will this question is closed.
The answer is: pass content-type HEADER. (or in rails use .json at the end of the url)

Save data from api request to db (JSON)

I sending messages from my rail app through third party and track the deliver details and store it in my database table.After sending messages third party post deliver details to my given URL,I got response to my URL like.
http://yourdomain.com/dlr/pushUrl.php?data=%7B%22requestId%22%3A%22546b384ce51f469a2e8b4567%22%2C%22numbers%22%3A%7B%22911234567890%22%3A%7B%22date%22%3A%222014-11-18+17%3A45%3A59%22%2C%22status%22%3A1%2C%22desc%22%3A%22DELIVERED%22%7D%7D%7D
Like following format,
data={
"requestId":"546b384ce51f469a2e8b4567",
"numbers":{
"911234567890":{
"date":"2014-11-18 17:45:59",
"status":1,
"desc":"DELIVERED"
}}}
I used following code in my controller to display data.
json = params["data"]["numbers"]
puts json
But it displays NULL. Now I want to save the data into database.Is there any Gem to be used or any other method is good.Am new to ROR.
Your issue is that params["data"] is a string, but you are treating it like a hash.
data = JSON.parse(params["data"])
puts data['numbers']

How to decipher ChicagoBoss Post param

I'm a newbie in ChicagoBoss web framework. I have a server which receives a POST request from another server where POST params is of the form:
<<"clientId=STRING_FOR_CLIENT_ID&userId=STRING_FOR_USER_ID&sessionToken=STRING_FOR_SESSION_TOKEN">>
All I have to do is to add clientSecret=CLIENT_SECRET_STRING to this POST params in my server and resend it to the server outside to retrieve access_token.
It would be great if someone suggest some basic code how this can be done.
Are you making the client secret string from the original post data? Anyway, if you can use ibrowse, then:
%% send form data as iolist
Data = <<"clientId=STRING_FOR_CLIENT_ID&userId=STRING_FOR_USER_ID&sessionToken=STRING_FOR_SESSION_TOKEN">>,
ibrowse:send_req("http://httpbin.org/post",
[], post,
[Data, <<"&clientSecret=CLIENT_SECRET_STRING">>]).

Make Rails parse JSON from XDR Cross-Domain requests with content-type: nil

I'm building a client side application which cross-domain posts JSON, using CORS, to a Rails 3.2 application on a different server. Unfortunately, we need to support IE9, which means that we're falling back to using XDomainRequest (XDR) to send our cross-domain requests in IE.
The major limitation of XDR is that it doesn't support setting headers like 'Content-type', which means the Rails server doesn't know that it's receiving JSON, so doesn't parse it.
I got around this briefly by using jQuery.param to send the data as url encoded form data. However, some of the data we need to send is nested GeoJSON, which gets garbled by jQuery.param, so I have to send the POST body as JSON.
In config/initializers/wrap_parameters.rb I've set:
ActiveSupport.on_load(:action_controller)
wrap_parameters format: [:json, nil]
end
… so ActionController nests parameters inside model_name when the content-type is nil.
However, I still need Rails to try to JSON.parse() the data from requests where "Content-type" => nil. I could just manually do this in the controllers which need it (which isn't very clean), but I was hoping I would be able to make Rails do it for all {content-type: nil} requests.
I've been looking through the Rails source, and what I've found doesn't make me very hopeful. This method seems to be hard coded to check for :json, and parse if so, meaning I can't modify this from config:
https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/params_parser.rb#L43
Can anyone come up with a clever solution?

Resources