I ran into this issue while writing request specs for a POST endpoint that processes incoming email payloads that include file attachments.
this is what the request looked like in the request spec:
post api_v1_email_processor_path, params: params, as: :json
where params[:attachment]
was:
fixture_file_upload("spec/support/fixtures/files/report.csv", 'text/csv',)
and when I added some breakpoints in the controller, this is what the attachment was being converted into:
{"original_filename"=>"report.csv", "tempfile"=>"#<File:0x0000000121d2bfc8>", "content_type"=>"text/csv"}
as you can see, the "tempfile"
was being cast to a string value of "<File:0x0000000121d2bfc8>"
when I emulated the same request in a controller spec, to my huge confusion, there was no casting, and the breakpoint showed the param (correctly) as:
#<ActionDispatch::Http::UploadedFile:0x000000012d559488 @tempfile=#<Tempfile:/var/folders/jg/n_0rknb97kvddb0jgh6r3_x40000gn/T/RackMultipart20221008-45122-mzyqg5.csv>, @original_filename="report.csv", @content_type="text/csv", @headers="Content-Disposition: form-data; name=\"attachment1\"; filename=\"report.csv\"\r\nContent-Type: text/csv\r\nContent-Length: 24931\r\n">]
after seeing L.Youl's answer https://stackoverflow.com/a/67987482/14926068, it got me thinking that the issue was with how the request payload was being formatted, prior to being sent to the server through the Rack middleware. and so I tried taking off the as: :json
:
post api_v1_email_processor_path, params: params
and bingo, I got the same result as with the controller spec.
after inspecting response.request.content_type
in my request spec, I found that when I took the as: :json
off, its value changed from "application/json"
to
"multipart/form-data; boundary=----------XnJLe9ZIbbGUYtzPQJ16u1"
for completeness, I checked response.request.content_type
in the controller spec as well, and found that even with as: :json
in the request, the content_type
was being set to "multipart/form-data"
, and that accounted for the discrepancy in behavior between controller and request specs - it seems that as: :json
only affects the "content-type"
in the request specs.
in retrospect, it was obvious - json cannot be used to send actual files. but it took me a long time to figure it out because the equivalent controller spec was working fine and this was literally the only endpoint in my Rails api-only app that expected non-json payloads