ruby-on-rails - Rails API - 期望显示已创建对象的 json。获取没有对象的 302 状态码

标签 ruby-on-rails ruby json rails-api

我有 2 个应用程序。一个普通 Ruby 应用和一个 Rails 应用,它从普通 Ruby 应用监听 POST 并显示已发布的数据。

我正在尝试为我的 Rails 应用程序设计一个基本的 API。我用法拉第POST一些 JSON到我的应用程序。该部分的工作方式是 我可以看到在 POST 之后正在创建对象

在我的 resp我期待看到与我刚刚创建的对象相关的内容。相反,我确实看到了 status=302 .

我怎样才能得到显示 success 的响应?也许一些带有创建对象的 json 会很好。真的,我只想要一个指示符 POST正确进行。

--- 来自 普通 Ruby 应用 的代码发布到我的 Rails 应用

conn = Faraday.new(:url => 'http://localhost:3000/api/v1/tests') do |faraday|
  faraday.request  :url_encoded             # form-encode POST params
  faraday.response :logger                  # log requests to STDOUT
  faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
end   

resp = conn.post do |req|
  req.headers['Content-Type'] = 'application/json'
  req.body = data.to_json
end  

当我检查 resp在 pry 动我得到这个

[1] pry(#<Object>)> resp => #<Faraday::Response:0x007fb8c3a317d0 @env= #<struct Faraday::Env method=:post, body="<html><body>You are being <a href=\"http://localhost:3000/tests/2\">redirected</a>.</body></html>", url=#<URI::HTTP:0x007fb8c34176b0 URL:http://localhost:3000/api/v1/tests>, request= #<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>, request_headers={"User-Agent"=>"Faraday v0.9.1", "Content-Type"=>"application/json"}, ssl= #<struct Faraday::SSLOptions verify=nil, ca_file=nil, ca_path=nil, verify_mode=nil, cert_store=nil, client_cert=nil, client_key=nil, certificate=nil, private_key=nil, verify_depth=nil, version=nil>, parallel_manager=nil, params=nil, response=nil, response_headers= {"x-frame-options"=>"SAMEORIGIN", "x-xss-protection"=>"1; mode=block", "x-content-type-options"=>"nosniff", "location"=>"http://localhost:3000/tests/2", "content-type"=>"text/html; charset=utf-8", "cache-control"=>"no-cache", "x-request-id"=>"62da5fdc-6f00-4f2a-9c93-c80f6e08c4f8", "x-runtime"=>"0.219049", "server"=>"WEBrick/1.3.1 (Ruby/2.2.0/2014-12-25)", "date"=>"Thu, 30 Jul 2015 16:54:07 GMT", "content-length"=>"95", "connection"=>"close", "set-cookie"=> "request_method=POST; path=/, _cimportal_session=QjdTN0lOaXBMSTZYT0hvOCsyOVozeW43VTRiSHZXTTBlakJFMmllTTNmdTYvbnd5QllWZnRnSDJhN2lHSDJxM3phSHZWU1VGcElSWEY0V0N3dXd6RHdzTW5leHFtY3hoNDNaTHRCZ2l3ODVaZTVIcXc0eDBnMHdKelhhN2lZeEdIYXNlWU9ZaThETkUvYnRTL293c0lKRGU1N3FrY2liN2t6anBCNlJMaXJFPS0tZmhIQmN0VXVvVEJ4VE5rQ3JLdWphZz09--965190ef9f588d95347f2ee25044eb79f3b5d289; path=/; HttpOnly"}, status=302>, @on_complete_callbacks=[]>

如您所见,底部有一个 status=302。我怎样才能得到 success这里还有一些代表对象的 json?

这里是 Rails 的主要部分 create方法

@test = Test.new(test_params)

respond_to do |format|
  if @test.save
    format.html { redirect_to @test, notice: 'Test was successfully created.' }
    format.json { render :show, status: :created, location: @test }
  else
    format.html { render :new }
    format.json { render json: @test.errors, status: :unprocessable_entity }
  end

我错过了什么? POST正在运行,但我希望看到成功的响应。

最佳答案

你有一行内容是:

format.json { render :show, status: :created, location: @test }

这是导致您看到的 302 的原因。

而是尝试类似的东西

format.json { render status: :created, json: @test }

这应该会返回 201 状态代码,并使用创建的新对象的 JSON 进行响应。

关于ruby-on-rails - Rails API - 期望显示已创建对象的 json。获取没有对象的 302 状态码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31730736/

相关文章:

javascript - Rails 3 中的 Ajax 页面加载正在破坏其他 JavaScript 功能..?

ruby-on-rails - `require' : no such file to load -- sqlite3/sqlite3_native (LoadError)

javascript - Rails - 从 javascript 调用 Controller

ruby-on-rails - 通过 API 作为页面发布到 Facebook 页面(具有管理员权限)

ruby - 如果 gem 安装不支持, native 扩展回退到纯 Ruby

java - 在 Java 中动态创建的 JSON 对象

java - 无法使用 jackson 将具有内部对象的JSON解析为Java对象

ruby-on-rails - 为什么 ActiveRecord 的自动保存对我的关联不起作用?

ruby-on-rails - 如何在我的 ec2 实例中使用 git 部署我的 ruby​​ on rails 应用程序?

json - 如何从 nsdictionary swift 获取数组