4
0

More than 1 year has passed since last update.

Insomniaの使い方

Last updated at Posted at 2022-11-11

この記事の目的

インターンでapi開発を行なっている際にInsomniaを使用した。使い方を忘れないようにメモを残しておく。
なんとなく理解している程度のため表現方法や理解がおかしいかもしれない。その際はコメント等で指摘してくれると助かります。

Docker側の操作

terminal
rails s -b 0.0.0.0

IP0.0.0.0にバインドさせながらサーバーを起動させる。

rails側のapi

ソースコード

event_controller.rb
class Api::EventsController < ApplicationController
  def index
    #binding.pry

    events = Event.all
    render json: events
  end

  def create
    @event = Event.new(event_params.merge({ hashed_url: invent_hash }))
    #binding.pry

    if @event.save
      render json: @event
    else
      render json: @event.errors
    end
  end

  def invent_hash
    #binding.pry

    root_str = event_params[:user_id].to_s + event_params[:category_id].to_s + event_params[:name]
    Digest::SHA256.hexdigest(root_str)
  end

  private

  def event_params
    params.require(:event).permit(
      :user_id,
      :category_id,
      :name,
      :is_public,
      :description,
      :delete_at
    )
  end
end

ファイル構成

スクリーンショット 2022-11-11 10.16.20.png

ルーティング

スクリーンショット 2022-11-11 10.18.01.png

insomniaの操作

スクリーンショット 2022-11-11 10.14.00.png
上記の写真のようにURLにするとgetリクエストが送れる。
スクリーンショット 2022-11-11 16.01.00.png
getではなくpostを送るとこのような返り値が来る。

気づき・気になったこと

  • progateで学んだ書き方よりも抽象的な分、リクエストとはみたいな部分を意識するきっかけになった
    - リクエストをテンプレートとして保存して置けるのでapi開発の時に楽にデバックを行うことができる

参考

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0