How can I post via curl to upload a file (image in this case) to active storage? The docs don't say exactly how to do it with curl, or with JS (e.g. Axios)
Something like this works (if I turn off the authenticity token (skip_before_action :verify_authenticity_token)):
 curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST -d '{"image": {"user_id":"1"}}' http://localhost:3000/images.json
That will post as expected like a normal curl JSON request.
Encoding the file in base64 like this looks promising:
(echo -n '{"image": {"user_id":"1", "picture":"'; base64 /Users/cj/Desktop/DesktopArchive/5vvyo4u8y8wz.jpg; echo '"}}') | curl -v -H "Content-Type: application/json" -H 'Accept: application/json' -X POST -d @-  http://localhost:3000/images.json
Although I get a ActiveSupport::MessageVerifier::InvalidSignature on the upload. I am suspecting the base64 is not the correct signature. What should I use to encode the image to be uploaded to active storage?
Edit:
Not doing it via JSON, but by form works:
 curl \
   -F "image[user_id]=1" \
   -F "image[picture]=@/Users/cj/Desktop/DesktopArchive/5vvyo4u8y8wz.jpg" \
   http://localhost:3000/images
But still, how do you do it via a json call such that it can be done via Axios or something?
my example is not same your image model. but you can change you code after show my example code https://github.com/x1wins/tutorial-rails-rest-api/blob/master/README.md#active-storage
model
class Post < ApplicationRecord
  has_many_attached :files
end
controller
  # posts_controller
  # POST /posts
  def create
    @post = Post.new(post_params)
    @post.files.attach(params[:post][:files]) if params.dig(:post, :files).present?
    set_category @post.category_id
    if @post.save
      render json: @post, status: :created, location: api_v1_post_url(@post)
    else
      render json: @post.errors, status: :unprocessable_entity
    end
  end
  # PATCH/PUT /posts/1
  def update
    @post.files.attach(params[:post][:files]) if params.dig(:post, :files).present?
    if @post.update(post_params)
      render json: @post
    else
      render json: @post.errors, status: :unprocessable_entity
    end
  end
  # DELETE /posts/:id/attached/:id
  def destroy_attached
    attachment = ActiveStorage::Attachment.find(params[:attached_id])
    attachment.purge # or use purge_later
  end
curl
curl -F "post[body]=string123" \
        -F "post[category_id]=1" \
        -F "post[files][]=@/Users/rhee/Desktop/item/log/47310817701116.csv" \
        -F "post[files][]=@/Users/rhee/Desktop/item/log/47310817701116.csv" \
        -X POST http://localhost:3000/api/v1/posts
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With