rootstrap / active-storage-base64

Base64 support for ActiveStorage
https://rootstrap.com
MIT License
160 stars 16 forks source link

Getting ActiveSupport::MessageVerifier::InvalidSignature #40

Closed hanishjadala closed 5 years ago

hanishjadala commented 5 years ago

Hi, Am getting this error ActiveSupport::MessageVerifier::InvalidSignature Rails API testing via postman, passing base64 image string to my endpoint Rails: 5.2.3 , Ruby: 2.6.3, active_storage_base64 : 0.1.4 User.rb class User < ApplicationRecord include ActiveStorageSupport::SupportForBase64

has_one_base64_attached :avatar end Endpoint: def update_profile profile_picture = params[:avatar]

if user.update(avatar: profile_picture) 
  render status: :ok, json: user, result: "Profile pic updated"
else
  render status: :unprocessable_entity, json: user.errors.full_messages
end

end Note: If I pass file type it is uploading to active storage but with base64 image giving error. You can see how am passing base64 string in below image and If file time it works. key is "avatar" and value is data:text/plain;base64, image string of base64 Screenshot from 2019-08-26 18-32-49

santib commented 5 years ago

Hey @hanishjadala I think the problem is that you should be sending { data: "data:text/plain;base64,[base64 data]" } (notice that it's a hash with the key data and its value it's the string with data URI format)

I'm thinking that maybe the README is not clear enough 🤔

hanishjadala commented 5 years ago

Hi @santib, thanks for quick response. I have tried both ways 1) key as data and value as data:text/plain;base64,[base64 data] 2) key as data and value as data:text/plain;base64, string_of_base64_image Still getting same error ActiveSupport::MessageVerifier::InvalidSignature endpoint: profile_picture = params[:data]

if user.update(avatar: profile_picture) 
  render status: :ok, json: user, result: "Profile pic updated"
else
  render status: :unprocessable_entity, json: user.errors.full_messages
end

Screenshot from 2019-08-26 21-25-50

santib commented 5 years ago

@hanishjadala Let me put it with an example:

In the controller: profile_picture = params[:avatar] being: params # => { avatar: { data: "data:image/jpeg;base64,/9j/4AAQSkZJRg..." } }

What I'm trying to say is that the profile_picture variable should be a hash with the data key present. In other words: profile_picutre = { data: "data:image/jpeg;base64,/9j/4AAQSkZJRg..." } }

hanishjadala commented 5 years ago

@santib Here what I have changed { "avatar": { "data": "data:image/png;base64,iVBORw0KGgoAA..... } } User.rb class User < ApplicationRecord include ActiveStorageSupport::SupportForBase64

has_one_base64_attached :avatar end Controller: profile_picture = params[:avatar] if user.update(avatar: profile_picture) render status: :ok, json: user, result: "Profile pic updated" else render status: :unprocessable_entity, json: user.errors.full_messages end Screenshot from 2019-08-26 22-14-11

santib commented 5 years ago

@hanishjadala So the error changed now?

I tried to reproduce your steps but it worked fine for me: image

Would you mind trying these same steps in the console rather than using Postman? Just to try to reduce the number of possible errors

hanishjadala commented 5 years ago

@santib sure, will do it and will get back to you.

hanishjadala commented 5 years ago

@santib Thanks for your time. It worked in rails console. But not sure why it is not working on Postman. So, I should ask my mobile dev to frame the hash for base64 image and hit the endpoint. Does this kind of framing of hash easy for a mobile dev. As this gem is mostly for API only. Now this is a single image, how about the multiple images. How do we pass it? has_many_base64_attached :documents

santib commented 5 years ago

@hanishjadala Not sure why it's not working for you in postman.. is it possible that you are missing to set the header Content-Type: application/json?

Regarding attaching many attachments it should be pretty much the same as with one. For example:

user.documents.attach(
  { data: "data:image/jpeg;base64,[base64 data]" },
  { data: "data:image/png;base64,[base64 data]" }
)

you can check examples here: https://github.com/rootstrap/active-storage-base64/blob/master/spec/user_file_spec.rb#L97

hanishjadala commented 5 years ago

@santib I have selected body as raw and JSON(application/json) so header will be Content-Type will be application/json only but did not work with Postman. Rails console worked.

Hope with this issue info, more people start using this gem in there Rails API. Thanks again for your time. Will get back if there are any other issues.

user.documents.attach( [{ data: "data:image/jpeg;base64,[base64 data]" }, { data: "data:image/png;base64,[base64 data]" }] ) Above format multiple images can be saved

santib commented 5 years ago

Awesome 👌