2

I am creating an API endpoint which contains a file upload field and a few string fields. My goal is to allow clients to clear values on those string fields, i.e. the DB should persist these values as null.

However, due to the fact that the request may contain files, the client should be setting the Content-type header to multipart/form-data. This implies that client cannot send a representation of "null", but can only send an empty string to indicate the intent of clearing the value for a given string field.

Is there a way for grape-api library to know that when it is receiving a multipart request it should be able to nullify blank string values in the params, or is there a better approach to what I am trying to achieve?

Martin Verdejo
  • 1,229
  • 2
  • 13
  • 24

1 Answers1

1
Grape.configure do |config|
  config.param_builder = Grape::Extensions::Hashie::Mash::ParamBuilder
end

you can override the param builder. extend the default one and override the build_params method or monkey patch it.

params.transform_values {|v| v.eql?('') ? nil : v }
Oshan Wisumperuma
  • 1,808
  • 1
  • 18
  • 32