How to send two responses in Rails - ruby-on-rails

I am looking to create an application that accepts an uploaded photo, writes the photo, then uploads it to a backup server. I would like it to send to the browser a JSON response stating processing: true when it's uploading to the backup server, and then success: true when it successfully uploads to the backup server.
I know about response.stream.write in Rails, but when I use this, it sends one response with both of my json objects instead of two responses with one json object each. How do I do two separate responses? Do I need to request the page twice or is there a way for the page to send the request automatically?

See this stackoverflow post How to "stream" json from server to client using javascript. It's about your problem and it was answered.

Related

JMeter- POST Json Request does it simulate or actually post data in UI?

I know it is a basic Question. Does a POST Json Request for update in User interface actually Post data in to database or just simulates the load for Post data without actually posting in database. But can someone please clarify
An HTTP Post request with the mandatory body (can be parameters, JSON, XML etc.), is intended to upload the data into server (upload images, Sign Up etc.), or to post data which server wants (to validate Sign in etc., not necessarily insert into the DB). So, it is basically how Server treats the data and the purpose.
If server puts the received data into the database, when performed the action using the browser, then the same operation expected when performed through JMeter also irrespective of the type of body data (JSON, XML etc). So, If you post the JSON data, it must be inserted into DB.
In DB, One thing to note is that server, either can directly dump the JSON data as JSON type itself or parse the JSON data and take the required values and store them in the Database. It depends on how the server is implemented.
So, how the server is implemented, it should behave the same way, whether you send the request from the browser or JMeter.
It depends on implementation. From JMeter's perspective API endpoint is yet another URL, JMeter sends a request to it, measures time between request and response and marks result as passed or failed depending whether HTTP Status Code is below 400 or not.
So it is up to you to check:
What does API endpoint actually do
What is correct request syntax (mandatory arguments, headers, cookies, URL parameters, whatever)
What is the expected result.
Optionally, what happens if "bad" request is being sent.
When designing a JMeter test always run it with 1-2 users and View Results Tree listener enabled to ensure that it does what it is supposed to be doing.
Coming back to your question: if HTTP response code is below 400, JMeter will mark sampler as successful, it won't check response body or database so I would recommend using the following test elements for confirmation:
JDBC PostProcessor - to check whether database was updated as a result of the request or not.
Response Assertion - to check that API response doesn't have errors, status code, variables, returned from the database, etc.

How to get a file from rails-api which using carrierwave?

I have a rails-api project, which provide the api to access my data.
I use carrierwave to store my file, my model called User and file attribute called image.
So, the image attribute contained the file_name, url and some other info.
In order to translate the file through the api, I added the gem carrierwave-base64.
I understand the Upload process. The client app encode the file to base64 code, then sent to backend by a json message. For example:
{user: {email: "test#email.com", image: "data:image/jpg;base64,#{base64_image}"}
So when the backend receive the json request, the carrierwave will parse the base64 code to a file and store it to local or S3
What I do not understand, is the Download process:
When I request the user info, what I assume is that the image file would be transfered as a base64 code in a json message, and then the client app will encode the base64 code to a file(image), and then display.
But actually, what I can provide for the json data, is the file url, not the base64 code.
The reason I want to get the file(image) from the api-server is because I don't want to the client app directly access s3 by url. So every time when the client app want to get a file, it will request the api-server, and api-server will get the file and transfer to the client.
Does anyone can explain how to do the download?
Or if I was thought in a wrong strategy, that I need another api endpoint to response a file object, not just accompany with user model.
Cheers.
Restricting Access to Objects Stored on Amazon S3
https://github.com/thoughtbot/paperclip/wiki/Restricting-Access-to-Objects-Stored-on-Amazon-S3
you did a good thing with uploading ,But while downloading you need to send URL no base64 and its traditional
Also for securrity purpose you can put public read permission on s3 while uploading and use expiring_url(60, :thumb) for your clients
In this URL get expired in time that you have specified

Ruby on Rails: Capturing JSON data from webhook?

I'm trying to understand how webhook works. My understanding is that its the ability to connect two different applications. If I submit a webhook with url
localhost:3000/receiver
to one application, and I have my application with a method
def receiver
end
I was wondering if I don't know what the callback is from the webhook would be, how would I capture data? How do I save any JSON data thats communicating with my application? I was thinking maybe save some file to see what the objects are, but I'm still fairly new and not sure how to capture JSON data?
Thanks
If you are sure that the webhook is returning a JSON, you can so something like this
data_json = JSON.parse request.body.read
Sure, a webhook a is tool to sincronize two apps
You HAVE tou know the structure of the incoming json, because you need to get the info inside
By definition a webhook is sent by POST method, so you can capture it just inspecting the body of the petition, i.e.
webHook = JSON.parse(params[:something])
Your would try with github web hooks and publish your app in heroku, the api is very well documented and there are many examples.

Communication with a web server

I am working on an application for iOS which needs to synchronize data between multiple users and one web server.
I did some research and the best solution I found for this is working with JSON.
At the moment I have a .json file on a web server and I am able to download the data to my iphone app and parse it.
I have two problems I didn't manage to understand yet:
1. How can each iphone user write back data to the json file on the server?
2. How can I take a data table and automaticly convert it to a .json file? so the .json file on the server will be updated every few minutes or so...
I'll appreciate any help with this!
You dont send JSON file as afile to the devices, you send a JSON request, I advice to do more reading about this topic, you can start from below:
Working with JSON in iOS 5 Tutorial
enter link description How to make HTTP request from iPhone and parse JSON result
How to fetch and parse JSON by using data models
and read this SO question:
iPhone/iOS JSON parsing tutorial
How to sent JSON request in the Post method in iPhone?
How to send json data in the Http request using NSURLRequest

Multipart response for web service

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.

Resources