Search code examples
ruby-on-railsrubypostmanrails-activestorage

Rails-API How to test Active Storage


I added Active Storage into my Rails API application and want to test it, but I don't know how to do it. I was trying send file with JSON data in Postman, but JSON data doesn't send correctly or I am doing something wrong. I did it like that: Image from postman

Is there any option to send request with file and JSON data without creating any view?


Solution

  • As far as I know, an upload can't be done via an API rest way. You need to use enctype: multipart/form-data as regular POST done via form.

    In your screenshot you are already sending as form-post, because you chose to send via form-data. This is why your json isn't properly parsed by rails.

    If you want to upload an image and post data in the same request you will need to break your json attributes into form data fields like:

    data[name]=asaa
    data[last_name]=foo
    ...
    

    Or, you can send a JSON in your data field and do the manual parsing when fetching in controller like:

    
    def upload
      file = params[:file]
      data = parsed_params_data!(params)
    
      # do your magic
    end
    
    def parsed_params_data!(params)
      JSON.parse(params[:data])
    end