I'm am new to rails. I want to consume my rest API with an android application. So I want to test whether my controller handles POST request or not. I'm using Advanced REST client for chrome to make a POST request.
Errors
Bad request 400 - rest client (chrome)
Update 1: Log output:
Started POST "/android" for 127.0.0.1 at 2015-04-17 00:51:09 +0530 Error occurred while parsing request parameters. Contents:
{tablet_id:1,pulse:2,pulse_timestamp:'NOW()'}
ActionDispatch::ParamsParser::ParseError (795: unexpected token at '{tablet_id:1,pulse:2,pulse_timestamp:'NOW()'}'):
My rails controller:
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
Check out the rails routing guides . Specifically at the section where it defines the types of HTTP verbs created for a controller. By default the only POST
is :create
. But you can edit your routes to explicitly allow POSTs for your own custom APIs by changing Routes.
Maybe this would help:
def pulse_feedback_params
params.require(:pulse_feedback).permit!
end
But of course server log output will be very helpful.
Thank you Guys, after lot of head breaking got it working. The problem was with my application_controller.rb and my json format above.
I had to comment this line
before_action :authenticate_user!
in my application_controller.rb file
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
It wasn't accepting POST request from unauthorized clients(as I was using a chrome-addon to make POST request)
And my json format should have been:
{
"pulse_feedback": {
"tablet_id": "1",
"pulse": "2",
"pulse_timestamp": "NOW()"
}
}
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.