Heroku Rails API 使用 Post 获取请求给出 500 内部服务器错误
Heroku Rails API giving 500 Internal Server Error with Post fetch request
我正在尝试向上传到 Heroku 的 Rails API 发出 POST 请求。
目前我正在测试邮递员,只是为了确保它在继续测试 heroku 应用程序之前在那里工作。
在本地,我能够与我的 React 前端和我的 Rails API 后端进行交互。本地主机没有问题。
URL: https://appname.herokuapp.com/api/v1/users/
headers:
key: Access-Control-Allow-Origin, value: *
//I have also removed the access control allow origin so with and without.
key: Content-type, value: application/json
为创建用户而传入的数据确实已创建,但 return 响应是 500 内部服务器错误
[
{
"id": 1,
"first_name": "Roger",
"last_name": "Perez",
"email": "roger@yahoo.com",
"birthday": "2000-10-10",
"gender": "male",
"avatar": null,
"home_base": null,
"initials": null,
"comments": [],
"events": [],
"user_events": [],
"messages": []
},
Rails
class Api::V1::UsersController < ApplicationController
# before_action :requires_login, only: [:index]
# before_action :requires_user_match, only: [:show]
def index
@users = User.all
render json: @users
end
def create
puts "#{params}"
@user = User.new(get_params)
@verify_user = User.find_by(email: params[:email])
@user.email = params[:email]
@user.password = params[:password]
# if (@user.save)
if (@verify_user === nil && @user.save )
token = generate_token
render json: {
message: "You have been registed",
token: token,
id: @user.id,
status: :accepted
}
elsif (@verify_user.valid?)
render json: {
message: "You already have an account",
errors: @user.errors.full_messages,
verify: @verify_user,
status: :conflict}
else
render json: {
errors: @user.errors.full_messages,
status: :unprocessable_entity
}
end
end
private
def get_params
params.permit(:first_name, :last_name, :email, :password, :birthday, :gender,)
end
end
宝石文件
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.3.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem "rails_12factor", group: :production
gem 'jwt'
gem 'bcrypt', '~> 3.1.7'
gem 'rest-client', '~> 2.0', '>= 2.0.2'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
gem 'http'
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors'
gem 'active_model_serializers', '~> 0.10.0'
gem "dotenv-rails"
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
日志
2018-07-20T17:56:18.618067+00:00 heroku[router]: at=info method=POST path="/sessions" host=meetfriends-api.herokuapp.com request_id=e6471540-3038-46d8-ae68-3c671266ecd8 fwd="71.190.202.18" dyno=web.1 connect=0ms service=98ms status=500 bytes=203 protocol=https
cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*',
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
如果我遗漏了任何细节,请告诉我。感谢您的帮助。
因为这是我第一次将应用程序上传到 Heroku,所以我忽略了一个简单的错误。
我需要为应用程序设置环境变量才能正常工作,因为我使用的是 JWT Auth 并且我的 .env 文件中有密钥,这些密钥没有上传到我的 github.
https://devcenter.heroku.com/articles/config-vars
配置 env 变量后,我不再收到 500 内部服务器错误。
来自文档:
查看当前配置变量值
heroku config
GITHUB_USERNAME: joesmith
OTHER_VAR: production
heroku config:get GITHUB_USERNAME
joesmith
Set a config var
heroku config:set GITHUB_USERNAME=joesmith
Adding config vars and restarting myapp... done, v12
GITHUB_USERNAME: joesmith
Remove a config var
heroku config:unset GITHUB_USERNAME
Unsetting GITHUB_USERNAME and restarting myapp... done, v13
您也可以通过仪表板进行配置。
我正在尝试向上传到 Heroku 的 Rails API 发出 POST 请求。 目前我正在测试邮递员,只是为了确保它在继续测试 heroku 应用程序之前在那里工作。 在本地,我能够与我的 React 前端和我的 Rails API 后端进行交互。本地主机没有问题。
URL: https://appname.herokuapp.com/api/v1/users/
headers:
key: Access-Control-Allow-Origin, value: *
//I have also removed the access control allow origin so with and without.
key: Content-type, value: application/json
为创建用户而传入的数据确实已创建,但 return 响应是 500 内部服务器错误
[
{
"id": 1,
"first_name": "Roger",
"last_name": "Perez",
"email": "roger@yahoo.com",
"birthday": "2000-10-10",
"gender": "male",
"avatar": null,
"home_base": null,
"initials": null,
"comments": [],
"events": [],
"user_events": [],
"messages": []
},
Rails
class Api::V1::UsersController < ApplicationController
# before_action :requires_login, only: [:index]
# before_action :requires_user_match, only: [:show]
def index
@users = User.all
render json: @users
end
def create
puts "#{params}"
@user = User.new(get_params)
@verify_user = User.find_by(email: params[:email])
@user.email = params[:email]
@user.password = params[:password]
# if (@user.save)
if (@verify_user === nil && @user.save )
token = generate_token
render json: {
message: "You have been registed",
token: token,
id: @user.id,
status: :accepted
}
elsif (@verify_user.valid?)
render json: {
message: "You already have an account",
errors: @user.errors.full_messages,
verify: @verify_user,
status: :conflict}
else
render json: {
errors: @user.errors.full_messages,
status: :unprocessable_entity
}
end
end
private
def get_params
params.permit(:first_name, :last_name, :email, :password, :birthday, :gender,)
end
end
宝石文件
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.3.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem "rails_12factor", group: :production
gem 'jwt'
gem 'bcrypt', '~> 3.1.7'
gem 'rest-client', '~> 2.0', '>= 2.0.2'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
gem 'http'
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors'
gem 'active_model_serializers', '~> 0.10.0'
gem "dotenv-rails"
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
日志
2018-07-20T17:56:18.618067+00:00 heroku[router]: at=info method=POST path="/sessions" host=meetfriends-api.herokuapp.com request_id=e6471540-3038-46d8-ae68-3c671266ecd8 fwd="71.190.202.18" dyno=web.1 connect=0ms service=98ms status=500 bytes=203 protocol=https
cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*',
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
如果我遗漏了任何细节,请告诉我。感谢您的帮助。
因为这是我第一次将应用程序上传到 Heroku,所以我忽略了一个简单的错误。 我需要为应用程序设置环境变量才能正常工作,因为我使用的是 JWT Auth 并且我的 .env 文件中有密钥,这些密钥没有上传到我的 github.
https://devcenter.heroku.com/articles/config-vars
配置 env 变量后,我不再收到 500 内部服务器错误。
来自文档:
查看当前配置变量值
heroku config
GITHUB_USERNAME: joesmith
OTHER_VAR: production
heroku config:get GITHUB_USERNAME
joesmith
Set a config var
heroku config:set GITHUB_USERNAME=joesmith
Adding config vars and restarting myapp... done, v12
GITHUB_USERNAME: joesmith
Remove a config var
heroku config:unset GITHUB_USERNAME
Unsetting GITHUB_USERNAME and restarting myapp... done, v13
您也可以通过仪表板进行配置。