【问题标题】:how to post JSON data to my rails app?如何将 JSON 数据发布到我的 Rails 应用程序?
【发布时间】:2015-04-16 20:42:58
【问题描述】:

我是 Rails 新手。我想用一个 android 应用程序使用我的 rest API。所以我想测试我的控制器是否处理 POST 请求。我正在使用 chrome 的高级 REST 客户端发出 POST 请求。

错误

错误请求 400 - 休息客户端(chrome)

更新 1: 日志输出:

在 2015-04-17 00:51:09 +0530 为 127.0.0.1 开始 POST "/android" 解析请求参数时出错。 内容:

{tablet_id:1,pulse:2,pulse_timestamp:'NOW()'}

ActionDispatch::ParamsParser::ParseError(795: '{tablet_id:1,pulse:2,pulse_timestamp:'NOW()'}' 处的意外令牌):

我的导轨控制器:

class Android::PulseFeedbacksController < ApplicationController
  before_action :set_pulse_feedback, only: [:show, :edit, :update,:destroy]
  respond_to json
  # GET /pulse_feedbacks
  # GET /pulse_feedbacks.json
  def index
    @pulse_feedbacks = PulseFeedback.all
    respond_to do |format|
      format.json { render json: @pulse_feedbacks }
      format.xml { render xml: @pulse_feedbacks }
    end
  end

  def create
    @pulse_feedback = PulseFeedback.new(pulse_feedback_params)

    respond_to do |format|
      if @pulse_feedback.save
        format.html { redirect_to @pulse_feedback, notice: 'Pulse feedback was successfully created.' }
        format.json { render :show, status: :created, location: @pulse_feedback }
      else
        format.html { render :new }
        format.json { render json: @pulse_feedback.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_pulse_feedback
      @pulse_feedback = PulseFeedback.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def pulse_feedback_params
      params[:pulse_feedback]
    end
end

【问题讨论】:

  • 您的服务器日志将包含更多信息,可以准确地告诉您正在发生的事情(以及可能的问题)
  • @JanHøjriisDragsbaek 服务器错误是:解析请求参数时发生错误。内容:{pulse_feedback:{tablet_id:1,pulse:2,pulse_timestamp:'NOW()'}} 我是否以错误的格式发送 json 数据?
  • 您应该更新问题以包含日志输出。

标签: ruby-on-rails ruby json rest


【解决方案1】:

查看rails routing guides。特别是在它定义为控制器创建的 HTTP 动词类型的部分。默认情况下,唯一的POST:create。但是您可以通过更改 Routes 来编辑您的路由以明确允许您自己的自定义 API 的 POST。

【讨论】:

    【解决方案2】:

    也许这会有所帮助:

    def pulse_feedback_params
      params.require(:pulse_feedback).permit!
    end
    

    当然,服务器日志输出会很有帮助。

    【讨论】:

      【解决方案3】:

      谢谢你们,经过大量的头部破坏后,它开始工作了。 问题出在我的 application_controller.rb 和上面的 json 格式上。

      我不得不评论这一行

        before_action :authenticate_user!
      

      在我的 application_controller.rb 文件中

      class ApplicationController < ActionController::Base
            respond_to :html, :json
      
            # Prevent CSRF attacks by raising an exception.
            # For APIs, you may want to use :null_session instead.
            protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' }
            #before_action :authenticate_user!
      end
      

      它不接受来自未经授权的客户端的 POST 请求(因为我使用 chrome-addon 发出 POST 请求)

      而我的 json 格式应该是:

      {
          "pulse_feedback": {
              "tablet_id": "1",
              "pulse": "2",
              "pulse_timestamp": "NOW()"
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-12-20
        • 2011-12-24
        • 2019-09-11
        • 1970-01-01
        • 2021-03-17
        • 2016-05-21
        • 1970-01-01
        • 1970-01-01
        • 2016-01-01
        相关资源
        最近更新 更多