3

In a Rails 6 api, I want to test a controller with strong parameters with Postman.

In the controller:

class ProcessorController < ApplicationController
...
def create
    file = processor_params[:file]
...
end

private

def processor_params
    params.require(:upload).permit(:file)
  end
end

There is a simple .csv file I have to upload that is required by this request.
First line in the CSV is the column header names (comma separated). About 30 lines of csv data
CSV Headers are: date, hours worked, employee id, job group

Here is an example fixture that works that I want to reproduce in postman:

post '/processor', params: {
     upload: {
         file: fixture_file_upload('report.csv', 'text/csv')
     }
}

How do I correctly post the params and body so that the rails API returns a HTTP 200 response?

I have tried setting up the postman request a few ways, including (1) through a form-data body,
(2) enter image description here and
(3) this raw json:

{
    "upload": {
        "file": {
            "date": "{{date}}",
            "hours worked": {{hours worked}},
            "employee id": {{employee id}},
            "job group": {{job group}}
        }
    }
}

It's not working. I am always getting errors such as

 "status": 500,
    "error": "Internal Server Error",
#<NoMethodError: undefined method `permit' for \"file\":String>",
{
    "status": 400,
    "error": "Bad Request",
    "exception": "#<ActionController::ParameterMissing: param is missing or the value is empty: upload>"
Ridhwaan Shakeel
  • 981
  • 1
  • 20
  • 39

0 Answers0