Search code examples
androidruby-on-railsrubyrestful-architecture

Completed 422 Unprocessable Entity RESTful API Rails while uploading images


I have an Android app that takes pictures and send to RESTful API build on Ruby on Rails. While i upload image after authenticating from app, 422 unprocessable entity error occurs. I used carrierwave gem to handle images. Here is my User_controller.rb file

class UsersController < ApplicationController
  skip_before_action :authorize_request, only: :create
  #skip_before_action :verify_auth_token
  # POST /signup
  # return authenticated token upon signup
  def create
    user = User.create!(user_params)
    auth_token = AuthenticateUser.new(user.email, user.password).call
    response = { message: Message.account_created, auth_token: auth_token }
    json_response(response, :created)
  end

  def index
    json_response(@current_user)
  end

  private

  def user_params
    params.permit(
        :name,
        :email,
        :password,
        :password_confirmation,
    )
  end
end

images_controller.rb file

class ImagesController < ApplicationController
  before_action :set_user
  before_action :set_user_image, only: [:show, :update, :destroy, :create]

  def index
    json_response(@current_user.images)
  end

  def show
    json_response(@image)
  end

  def create
    @current_user.images.create(image_params)
    json_response(@current_user, :user_id)
  end

  private
  def image_params
    params.permit(:photo)
  end

  def set_user
    @current_user = User.find(params[:user_id])
  end

  def set_user_image
    @image = @current_user.images.find_by!(id: params[:id]) if @current_user
  end


end

Here are my model files. user.rb

# app/models/user.rb
class User < ApplicationRecord
  mount_uploader :photo, PhotoUploader


  # encrypt password
  has_secure_password
  has_many :images, foreign_key: :user_id

  # Validations
  validates_presence_of :name, :email, :password_digest

end

image.rb

class Image < ApplicationRecord
  mount_uploader :photo, PhotoUploader
  belongs_to :user
end

Routes.rb

Rails.application.routes.draw do
  resources :users do
    resources :images
  end
  post 'auth/login', to: 'authentication#authenticate'
  post 'signup', to: 'users#create'
  post 'images', to: 'images#create'
end

Error Log:

Started POST "/images" for 203.159.41.110 at 2017-07-03 21:44:56 +0700
Processing by ImagesController#create as HTML
  Parameters: {"photo"=>#<ActionDispatch::Http::UploadedFile:0x005615c5a900e8 @tempfile=#<Tempfile:/tmp/RackMultipart20170703-29818-3gozwl.jpg>, @original_filename="IMG_20170703_202628.jpg", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"photo\"; filename=\"IMG_20170703_202628.jpg\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n">, "auth_token"=>"token"}
Completed 422 Unprocessable Entity in 2ms (Views: 1.2ms | ActiveRecord: 0.0ms)

Solution

  • After a lot of research I found that auth token is not handled. Sending Auth token as header solved the issue httppost.setHeader("Authorization:", token);