简体   繁体   中英

Sending a POST request/response to a backend API from another device?

So I have this plan to use some raspberry PI Zero's to send data to a Rails backend API i'll be designing. This is to get more experience with Rails and just for a fun side project.

Im going to be reading sensor data on the Raspberry Pi's and sending them to my REST API (the data will be stored and then I can go to each "sensors" page on a dashboard and see historical values/timeline of temperature or whatever sensor I decide to use).

Im mostly familiar with HTTP requests...but usually only on the same server. And this made me realize I don't really understand "HOW" they work. (I just know that with Forms I need to use POST/GET for normal pages/etc....,and just general routing involving that).

That being said, I know on the Rails side i'll need to receive the data Probably via a POST request (Sort of like a form being submitted)...but im not sure how I should be sending it? As in what format? How should it be constructed?

I thought this was simple, but then the more I thought about it the more I realized I didn't know. I know that I need to send the data every x intervals (Via a CRON job running a python script) so i'd be sending a POST request to the API right? I guess I understand that a Form on a site in sending a POST request but what exactly are we requesting? And what would be returned from a Form POST request? Just a 200 status ok?

What about in the case of these devices: Sending a POST request to send their sensor data to be saved on the backend, should the server respond back to them?

Thanks, and sorry for the sorta scattered question. Im realizing I know a lot less than when I started learning rails. Just trying to clear up my understanding on http requests.

Send the data as JSON. Building the rails app is not really different from building any other JSON API.

Lets say you set your models up like so:

# app/models/measurement.rb
class Measurement
  belongs_to :unit
end

# app/units/unit.rb
class Unit
  has_many :measurements
end

You can use a token based authentication scheme to authenticate each PI unit. Or you can use shared secret such as a MAC address of the unit.

You can register each PI unit in the rails app via the rails console or create the routes and controllers for it in the rails app if you want to be thorough.

If you don't want to manually enter the rails db id for each PI unit you can create a "discovery route" where the PI sends its MAC address and gets an id.

To register measurements you just have send a POST request:

# /config/routes.rb
resources :units do
  resources :measurements, module: :units, only: [:create, :index]
end

# /app/controllers/units/measurements_controller.rb
module Units
  class MeasurementsController

    before_action :set_unit
    before_action :authenticate_unit! 

    # POST /units/:unit_id/measurements
    def create
      @measurement = @unit.measurements.new(measurement_params)
      if @measurement.save
         head :created, location: @measurement
      else
         head :unprocessable_entity
      end
    end

    # GET /units/:unit_id/measurements
    def index
      render json: @unit.measurements
    end

    private

    def authenticate_unit!
      # @todo check auth token in header.
      # should raise an exception and return 401 unauthorized if not authenticated
    end

    def set_unit
      @unit = Unit.find(params[:id])
    end

    def measurement_params
       params.permit(:temperature, :foo, :bar)
    end
  end
end

The Pi would send the data to POST /units/:unit_id/measurements and get a 201 - Created response or 422 - Bad entity . Sending a Location header is really optional since the PI is probally not going to do anything with the response.

How exactly the payload is formatted is up to you as an author. This example just uses a "flat" JSON object:

{
  temperature: 3,
  foo: 2,
  bar: 3
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM