Search code examples
ruby-on-railsfile-uploadpostmanrails-activestorage

Upload a file(.pdf, .jpg etc) using rails active storage via API through postman? (not through rails views)


I saw N number of tutorials on uploading a file to active storage locally, to S3 etc using rails view, but I cannot find a legit source on how to upload a file to an active storage through API Say via postman, like what are prerequisites required to pass an attachment through API via postman?

Let me detail it.

Step 1: Through React or any frontend framework, I will choose a file to upload(a .PDF file), ultimately it should be saved somewhere.

Step 2: This chosen file should be passed through as API to the backend service where the storage of the chosen file to be saved in a storage like AWS S3.

How will the API request be sent to store the file? Can someone help me out with it?


Solution

  • its Very Simple to Do Open the Postman
    Go to Body -> Form-data after that take a mouse to the key filed you will find Option for File

    Option for Text and File in Postman

    Then you have to select the file and send the request enter image description here

    Or send the File Path in the raw JSON object as well like this and then Create the Object at backend so place check on file object if it blank then send path like this

       // For Temporary Use Over Back End Only NOT NEED To Send These 
        "file_path" : "/home/jolly/Downloads/pdf/pdf-5.pdf",
        "file_name" : "pdf-5.pdf"
    

    and then in the code use it to create the file object which u pass where u needed to save s3 or rails storage

        file_path = params[:file_path]
        file_name = params[:file_name]
        image_path = Rails.root+file_path
        image_file = File.new(image_path)