0

I have author class using active storage.

              class Author < ApplicationRecord
                  has_one_attached :image
              end     

Here is my faraday connection

       conn = Faraday.new(
         url: 'http://localhost:3000/',      
         headers: {'Content-Type' => 
         'application/json','Authorization' => "Bearer 
         qAJiTqIJRou0"}    )      do   |f|

          f.request :multipart
          f.adapter :net_http

       end 

permit params

        def author_params
          params.require(:author).permit(:image, :title, 
                        :full_name)    
        end

post request

             require 'faraday'
             require 'faraday/multipart'

             payload = { author: {title: "lo"} }
             payload[:author][:image] = Faraday::Multipart::FilePart.new(Rails.root.join('app', 'assets',                  

                            'images', 'about.jpg'), 'image/jpeg')

             conn.post('/api/v1/authors', payload.to_json)

My received params

        Unpermitted parameter: :image. Context: { controller: 
        Api::V1::AuthorsController, action: create, request: # 
        <ActionDispatch::Request:0x00007f85583d7860>, params: 
         {"author"=>{"title"=>"foo iiiiiii", "full_name"=>"tony", "image"=>{"content_type"=>"image/jpeg", "original_filename"=>"about.jpg", "local_path"=>"/home/kashif/ticket_plot_api/app/assets/images/about.jpg", "io"=>"#<File:0x00007f78f416abd8>", "opts"=>{}}}, "controller"=>"api/v1/authors", "action"=>"create"} }

Author is saving without image

Is anything wrong?

Thanks

Kashiftufail
  • 10,815
  • 11
  • 45
  • 79
  • 1
    remove `'application/json'` header and don't call `to_json` on the payload, you're not sending json, you're sending multipart data. – Alex May 31 '23 at 15:48

2 Answers2

2

I can see two mistakes here:

  1. You have to open the file you pass to the Faraday::Multipart::FilePart like this:
Faraday::Multipart::FilePart.new(File.open(Rails.root.join('app', 'assets',                  
                                           'images', 'about.jpg')), 
                                'image/jpeg')
  1. You have to move the image into the author object
`payload[:author][:image] = Faraday::Multipart::FilePart.new(...)`
Jan Vítek
  • 671
  • 4
  • 11
  • not working.......... – Kashiftufail May 30 '23 at 13:17
  • 1
    @Kashiftufail you have to elaborate on that; please update your question at least with current state of received params; your controller method would also be helpful; is the `:image` a permitted attribute? – Jan Vítek May 30 '23 at 13:25
  • here is current params Unpermitted parameter: :image. Context: { controller: Api::V1::AuthorsController, action: create, request: #, params: {"author"=>{"title"=>"foo", "full_name"=>"tony", "image"=>{"content_type"=>"image/jpeg", "original_filename"=>"about.jpg", "local_path"=>"/home/kashif/ticket_plot_api/app/assets/images/about.jpg", "io"=>"#", "opts"=>{}}}, "controller"=>"api/v1/authors", "action"=>"create"} } – Kashiftufail May 30 '23 at 13:30
  • The first thing it says the reason why it is not working `Unpermitted parameter: :image`. Add `:image` to the permitted parameters in your controller. – Jan Vítek May 30 '23 at 13:33
  • it is done already def author_params params.require(:author).permit(:title, :full_name, :image) end – Kashiftufail May 30 '23 at 13:46
0

i just remove "Content-Type' => 'application/json" from header. This was issue as suggested by @Alex in comment.

 headers: {'Authorization' => "Bearer qAJiTqIJRou0"}    

And working.

Kashiftufail
  • 10,815
  • 11
  • 45
  • 79