I am trying send user profile images from a client to the server using ActiveResource. It looks like ActiveResource doesn't support multi-part.
Is there a gem/plugin that adds Multipart support to ActiveResource?
Or any other alternative approach to addressing this issue.
The best way is not to use multipart, but to embed the file's binary content in your resource's XML, which then goes in the request body.
This is easy to do using ActiveResource
I've created a plugin to do that in Rails 3:
http://github.com/nragaz/encoded_attachment
Related
I'd like to use Rails as a backend to a mobile app (Android and maybe iOS), and one of the requirements is file upload.
I haven't been able to find any resources on using Activestorage with direct upload in this way – does anyone have an example or tips?
The alternative – I suppose – is to reverse-engineer activestorage.js and do exactly what it does. But perhaps I'm not the first one with this need...
Rails does not care if your data is sent from a mobile-app, html-form, react-app, server-to-server...
If you can authenticate the request and send a multi-part file, then you do active-storage from your app.
Probably smart to look at gems/apps that do this already https://github.com/cbothner/react-activestorage-provider
I'm writting api for webservice, and i need to provide ability for users to upload files using this api.
For my uploads i'm using carrierwave, but not sure how do i pass file from api request to carrierwave, and how the file should be sent from client machine to server
Basically it is based on the tool which is used by the API consumer. If API consumer is using ruby then can consume it by passing File object, or using httmultiparty gem we can upload the file.
For you reference https://github.com/jwagener/httmultiparty. Please let me know if you need more help.
Usually I implement file uploads over a REST API by allowing clients to send a PUT request with the base64 encoded binary data of the file inside the request body.
Then you can route the client request to your CarrierWave uploader, which can decode the binary data contained in the request body using something like FilelessIO.new(Base64.decode64(encoded_file))
Try RestClient. It encapsulates net/http with cool features like multipart form data:
require 'rest_client'
RestClient.post('http://localhost:3000/foo',
:name_of_file_param => File.new('/path/to/file'))
It also supports streaming.
gem install rest-client will get you started.
In one web service written in Rails, I would like to answer with a file along with additional information.
For this, I consider respond with multipart data. How can I send a multipart response with a file and json?
If there is a better way to do this, please let me know. Note that is not possible add the extra data in the file I'm sending.
Extra points for the face of the problem, that is send a file and data at same time. I already accomplished that by doing a multipart request, but if is there a better way to do this, I would like to know.
I don't know exactly what kind of front end you are using and what your browser compatibility requirements are, or you need the webservice for integration with other apps only, but assuming you are communicating with server over ajax and your app is running in modern browser (or you are allowed to use flash plugin), you can return file contents as base64 encoded string as a part of json response. So in rails controller you would have something like this:
render json: {success: true, file: {name: 'invoice.pdf', data: Base64.encode64(#file.read), extra_info: 'some info'}}
on client side you can process that json, get all the metadata you need from it and also let user save the file to their computer. For that you can use flash plugin or native api implementation
This way you can return couple files with any metadata you want with them to user and on client side user can save files if needed. Also, same webservice can be consumed by other applications as long as they can parse json.
try using gem 'curb' or 'rest-client' gem.
https://github.com/rest-client/rest-client
and
https://github.com/taf2/curb
I'm sure you have done some googling already, have you seen this already? It seems like there is a Gem for what you are trying to accomplish, the Gem however seems to be pretty old.
My question is simple here. I want to send json data from one Rails app to other Rails app via POST. How do I achieve that?
You can find a complete architectural example of how to do this with ActiveResource here
If you just need a simple call, I'd suggest to use REST-client for the calling side.
The provider side is pretty standard. Just expose an action that responds to JSON posts. Let me know if you need more details.
Have one Rails app accept JSON via a POST action in a controller, and have the other Rails app submit the JSON via an HTTP library like rest-client.
Actionwebservice seems to be a generic web-service infrastructure for rails.
Is it possible to use this gem to pass image from server to the client and how to do that?
Thank you.
Finally, I find a workaround my self.
Take a image file for example, you can use send_file to send the file to the browser, or you can send out the link to the resource to the client.
Then the client could use the direct link to access the resource by a standard HTTP GET.