首页 > 解决方案 > 使用 Excon Rails 6.0.0 在生产中未发送 API 请求

问题描述

我遇到了一个问题,即我对 Excon gem 的 POST 请求没有在生产中发送,但在开发中完美运行。在我的日志中,它显示Processing ServiceFormsController#run_background_check 作为 HTML。我不知道为什么会发生这种情况......上周它工作得很好。现在突然之间,没有请求从生产服务器发出……它显示了正在发生的操作,但 API 端点从未收到 POST。我将该网站作为列入白名单的域添加到我的 CORS 配置中。这对改善结果没有任何帮助。

service_forms_controller.rb

class ServiceFormsController < ApplicationController
 @service_form = ServiceForm.find(params[:id])
 @service_info = @service_form.info

 if @service_info.first_name.present? && @service_info.last_name.present? && @service_info.ssn.present? && @service_info.address.present? && @service_info.email.present?

@report_builder = GoodHire.new
if Rails.env.production?
 @report_builder.create_report_for_candidate(@service_info.first_name, @service_info.last_name, @service_info.ssn, @service_info.address, @service_info.email)
# other mandatory fields added to this call
end
  redirect_to service_forms_path, notice: "You've submitted the candidate for a background check"
  else
redirect_to service_forms_path, notice: "The candidate doesn't have all fields completed"
end

good_hire.rb

class GoodHire
  API_KEY_PROD = 'ZZzazzee'

include RestClient
include Excon

def create_report_for_candidate(first_name, last_name, email, ssn, address, city, state, zip, work_histories =[].....,api_key = API_KEY_PROD)

begin
 Excon.post("https://api.goodhire.com/v1/Report", body: {
  "Candidate": {
  "FirstName": "#{first_name}"
  ....
  ...  
},
"Offer": {
  "Products": [
  "....ID"  
 ]
},
 "RequestOptions": {
  "SendPurchaseReceipt": true 
  }
}.to_json, headers: {"Content-Type" => "application/json", "Authorization" => "ApiKey #{api_key}"})
 rescue Excon::Errors => e
 puts "RESPONSE ERROR #{e.response}"
  end
end

服务器开发日志

Started POST "/service_forms/7/run_candidate_bckground_check?id=7" for 127.0.0.1
Processing by ServiceFormsController#run_candidate_background_check as HTML
Parameters: {"authenticity_token" => "zregegegergergergegegege", "id" => "7", "service_form_id"=> "7"}

app/controllers/service_forms_controller.rb: in `run_candidate_background_check'
Redirected to https://127.0.0.1:3000/service_forms 
Completed 302 Found in 4359ms

Started GET "/service_forms" for 127.0.0.1

initate_background_check.html.erb

<body>
 <%= button_to service_form_run_candidate_background_check(service_form_id: @service_form.id, id: @service_form.id), method: :post, class: 'bttn-simple bttn-sm bttn-primary'do %>
  Start Background Check
<% end %>
</body>

标签: ruby-on-railsrubyexcon

解决方案


API 验证导致 API 请求失败


推荐阅读