Rails-API How to test Active Storage - ruby-on-rails

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

Related

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']

Generate XML dynamically and post it to a web service in Rails

I am currently developing a Rails app in which I need to dynamically send XML request to an external web service. I've never done this before and I a bit lost.
More precisely I need to send requests to my logistic partner when the status of an order is updated. For instance when an order is confirmed I need to send data such as the customer's address, the pickup address, etc...
I intended to use the XML builder to dynamically generate the request and Net:HTTP or HTTParty to post the request, based on this example.
Is that the right way to do so? How can I generate the XML request outside the controller and then use it in HTTParty or Net:HTTP?
Thanks for your help,
Clem
That method will work just fine.
As for how to get the XML where you need it, just pass it around like any other data. You can use the Builder representation, which will automatically convert to a String as appropriate, or you can pass around a stringified (to_s) version of the Builder object.
If, for example, it makes sense for your model (which we'll call OrderStatus) to generate the XML, and for your controller to post the request:
# Model (order_status.rb)
def to_xml
xml = Builder::XmlMarkup.new
... # Your code here
xml
end
# Controller (order_statuses_controller.rb)
def some_method
#order_status = OrderStatus.find(:some_criteria)
... # Your code here
http = Net::HTTP.new("www.thewebservicedomain.com")
response = http.post("/some/path/here", #order_status.to_xml)
end
You may want to wrap the HTTP calls in a begin/rescue/end block and do something with the response, but otherwise it's all pretty straightforward and simple.
Make XML with Builder, then send it down the wire.
In your case it sounds like you may need to send several different requests as the order evolves; in that case:
Plan out what your possible order states are.
Determine what data needs to be sent for each state.
Decide how to represent that state within your models, so you can send the appropriate request when the state changes.
Where my example uses one method to generate XML, maybe you'll want 5 methods to handle 5 possible order states.

Configure Ember to send JSON to Rails Server

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.

Finding form fields in a Rails POST request

I'm using a plain old HTML form to send a post request to a rails app. Inside the app I would like to grab some of the input fields. I'm having trouble finding them inside the request.
Trying this:
logger.debug "Request Headers #{request.headers.inspect}"
Spits out a massive amount of data but I cannot find my form fields in there. Does anyone know where I can find them?
You can grab the request parameters submitted using:
logger.debug params.inspect
That will show all the form data submitted.

Resources