title: Upload file to S3 using Rails 5 API
body: <p>I have a controller method which is called on form submit. This method then calls the api for saving the data which also includes a file.
  If I execute the code for file upload in my controller method before calling the api, the file gets uploaded on s3 whereas if I try to upload the file in the api, it throws an error in the controller method.</p>
  
  <p>Example:
  This works:</p>
  
  <pre><code>def formsubmitmethod
  # Code for uploading a file to s3
  Aws.config.update(
  region: 'region_name',
  credentials: Aws::Credentials.new('access_key_id','secret_access_key')
  )
  s3 = Aws::S3::Resource.new(region: 'region_name')
  original_filename = params['picture'].original_filename
  obj = s3.bucket('bucket_name').object(original_filename)
  
  File.open(original_filename, 'wb') do |file|
  obj.put(body: params['picture'])
  end
  </code></pre>
  
  <h1>Code for calling the api where the data needs to be saved.</h1>
  
  <pre><code>@response = HTTParty.post("url_for_post_request_api", :body =&gt; { :query =&gt; params.to_json})
  end
  </code></pre>
  
  <p>But if I add the same code in the method for post_request_api(url_for_post_request_api) it throws the following error:</p>
  
  <pre><code>Encoding::UndefinedConversionError in controller method
  "\x89" from ASCII-8BIT to UTF-8
  Stack trace of parameters:
  Parameters:
  {"picture"=&gt;
  #&lt;ActionDispatch::Http::UploadedFile:0x007f60b6559498
  @content_type="image/png",
  @headers="Content-Disposition: form-data; name=\"picture\"; filename=\"1.png\"\r\nContent-Type: image/png\r\n",
  @original_filename="1.png",
  @tempfile=#&lt;File:/tmp/RackMultipart20161102-10689-137nvix.png&gt;&gt;
  }
  </code></pre>
  
  <p>Note: the other parameters in the form are also a part of the parameters displayed but I have removed it for readability.</p>
  
  <p>Also I would like to know how we can retrieve the file so that it can be displayed and can be downloaded.</p>