janko / uppy-s3_multipart

Provides Ruby endpoints for AWS S3 multipart uploads in Uppy
https://uppy.io/docs/aws-s3/
MIT License
65 stars 21 forks source link

Bad request on post #9

Closed mrsweaters closed 5 years ago

mrsweaters commented 5 years ago

I'm seeing an odd issue where the post request to /s3/multipart is failing on this line: https://github.com/janko/uppy-s3_multipart/blob/master/lib/uppy/s3_multipart/app.rb#L36

It looks like a call to r.params is failing.

Integration

# config/initializers/uppy.rb
require 'uppy/s3_multipart'

resource = Aws::S3::Resource.new(
  credentials: AWS_CREDENTIALS,
  region: 'us-east-1',
)

bucket = resource.bucket(Rails.application.secrets.aws_media_bucket)

UPPY_S3_MULTIPART_APP ||= Uppy::S3Multipart::App.new(
  bucket: bucket,
  prefix: 'ingress',
  public: false
)
# config.ru
require_relative 'config/environment'

run Rails.application

map '/s3/multipart' do
  run UPPY_S3_MULTIPART_APP
end
# config/routes.rb
namespace :api, defaults: { format: 'json' }, path: (Rails.env.development? ? '' : 'api') do
    mount UPPY_S3_MULTIPART_APP => '/s3/multipart'
end 

I'm posting through a webpack-dev-server proxy in development. Front end route is /api/s3/multipart which routes to /s3/mulipart on the backend. If I throw a log in the the r.post block in s3_multipart/app.rb I see that the request is making through the proxy. Not sure why it is failing on r.params.

mrsweaters commented 5 years ago

What's interesting is that if I log r.body.string in app.rb right about line 36 I get the correct params payload:

{"filename":"SampleVideo_1280x720_1mb.mp4","type":"video/mp4"}

But as soon as I call r.params it fails silently.

Logging r.env. shows that the correctly content type of application/json has been set.

Fails on this line in Roda: https://github.com/jeremyevans/roda/blob/master/lib/roda/plugins/json_parser.rb#L81

It's odd, looks like you are using the standard parser. When I call JSON.parse directly on r.env['rack.input'].read it works.

mrsweaters commented 5 years ago

I figured it out! I had the Oj gem installed and it was conflicting with Roda.

Had this in my Oj initializer:

require 'oj'
Oj.default_options = { mode: :compat, symbol_keys: true, use_to_json: true }
Oj.mimic_JSON()
Oj.add_to_json(
  Array, BigDecimal, Complex, Date, DateTime,
  Exception, Hash, Integer, OpenStruct, Range,
  Rational, Regexp, Struct, Time
)

Removing Oj solved the problem.