M
M
MagoVinch2014-12-19 02:25:01
Ruby on Rails
MagoVinch, 2014-12-19 02:25:01

Why does POST request to ruby ​​on rails from Advanced Rest Client not work?

The following method is described in the controller

def create
      @messege = Messege.new(messege_params)
      if @messege.save
        render json: @messege, status: :created, location: @messege
      else
        render json: @messege.errors, status: :unprocessable_entity
      end
  end


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

    # Never trust parameters from the scary internet, only allow the white list through.
    def messege_params
      params.require(:messege).permit(:author_id, :destin_id, :content)
    end
end

When I try to create a new message, when sending a POST request from the Advanced Rest Client, but returns a Bad Request , I
send it to test.com/messeges (I also tried test.com/messeges.json) and
fill in the parameters like this author=3&destin=1&content=test
" Content-Type" chose application/json
Suggest a solution!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Vsk, 2014-12-19
@viktorvsk

If you want to send JSON, then, in fact, send JSON.
You can check JSON, for example, here: jsonformatter.curiousconcept.com
This JSON: Check
will fail.
And this one:
Rather, yes.
Plus, sometimes you need to remember that JSON, whose arguments are taken in single quotes, can be considered invalid.
This is like the most common problem when using any rest-clients - they do not translate the convenient key / value format into JSON, unfortunately.
If this does not help, then describe the error in more detail. Because Obviously, it’s not a Bad Request that is returned to you, but some kind of exception, in fact.

B
Boris Penkovsky, 2014-12-19
@Able1991

The require method ensures that a specific parameter is present, and if it's not provided, the require method throws an error. It returns an instance of ActionController::Parameters for the key passed into require.

Your messege_params method checks for the presence of the :messege parameter and takes the :author_id, :destin_id, :content fields from it, so the request parameters must match
messege : { author_id:1, destin_id:2, content:3}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question