How to send data from Matlab to Rails

I am very new to Rails and web development.

I am creating a bunch of objects in Matlab, and I would like to send these objects to the database in my Rails application. Can anyone advise me how to do this?

So far, at the end of Rails, I have created basic scaffolding for my data. I can add objects to my database using the form in '/ myobjects / new'.

At the end of Matlab, I try to add objects using HTTP POST requests, for example:

s = urlread('http://localhost:3000/myobjects.json','POST',{'myobject','{name1:''value1''}'}) 

This fails and displays the following on the Rails console:

 Started POST "/myobjects.json" for 127.0.0.1 at 2012-06-16 11:48:28 -0400 Processing by MyobjectsController#create as JSON Parameters: {"myobject"=>"{name1:'value1'}"} WARNING: Can't verify CSRF token authenticity Completed 500 Internal Server Error in 1ms NoMethodError (undefined method `stringify_keys' for "{name1:'value1'}":String): app/controllers/myobjects_controller.rb:43:in `new' app/controllers/myobjects_controller.rb:43:in `create' 

This approach may not work, but hopefully the code above makes my goal clear. Can someone tell me how to fix my code, or suggest a better strategy for putting my data on the rails?

EDIT

At the moment, my new and created methods look like this (but I can change them as needed)

 # GET /irs/new # GET /irs/new.json def new @ir = Ir.new respond_to do |format| format.html # new.html.erb format.json { render json: @ir } end end # POST /irs # POST /irs.json def create @ir = Ir.new(params[:ir]) respond_to do |format| if @ir.save format.html { redirect_to @ir, notice: 'Ir was successfully created.' } format.json { render json: @ir, status: :created, location: @ir } else format.html { render action: "new" } format.json { render json: @ir.errors, status: :unprocessable_entity } end end end 
+3
source share
2 answers

In the end, I gave up trying to do this using the Matlab built-in functions. Instead, I imported the Java library ( Apache HttpComponents ). Here's the script I came up with. It did the job.

 javaaddpath(['utils/httpcomponents-client-4.2/lib/httpcore-4.2.jar']); javaaddpath(['utils/httpcomponents-client-4.2/lib/httpclient-4.2.jar']); import org.apache.http.impl.client.DefaultHttpClient import org.apache.http.client.methods.HttpPost import org.apache.http.entity.StringEntity httpclient = DefaultHttpClient(); httppost = HttpPost('http://127.0.0.1:3000/myobjects.json'); httppost.addHeader('Content-Type','application/json'); httppost.addHeader('Accept','application/json'); params = StringEntity('{"field1":"value1"}'); httppost.setEntity(params); response = httpclient.execute(httppost); 
+3
source

You can avoid this particular problem by setting

 class MyobjectsController < ApplicationController protect_from_forgery :except => :create ... end 

inside your controller. It disables the validation of the CSRF token.

+1
source

Source: https://habr.com/ru/post/954056/


All Articles