Rails functional test: sending URL query parameters in POST request

前端 未结 5 1410
栀梦
栀梦 2021-02-19 20:55

I\'m sending a POST request in a Rails functional test like this:

post :create, collection: { name: \'New Collection\' }

collection

5条回答
  •  难免孤独
    2021-02-19 21:47

    You want to send a POST request:

    I'm sending a POST request in a Rails functional test like this:

    But you want to retrieve data from a GET request:

    But, :api_key never appears in the request.GET hash on the server.

    A request cannot be GET and POST at the same time, if you are sending a POST request and pass parameters in the query string then you would have those parameter values available on a POST request, GET just won't have anything.

    Then:

    @request.GET[:api_key] = 'my key'
    post :create, collection: { name: 'New Collection' }
    

    You are modifying the GET values on the request, but then you actually send a POST request which means that when the post method gets called and the request is sent to the server only what you sent on the POST will be available. Just send the api key bundled with the POST request (could be inside the collection hash for that matter)

提交回复
热议问题