【发布时间】:2019-08-18 00:21:45
【问题描述】:
我对 Rails 中的 API 非常陌生,虽然我已经获得了一些关于如何构建 HTTParty 发布请求的帮助,但是我发送的有效负载(数据)不会影响我的 API 数据库
我只想通过我的应用程序的 POST 请求在 API 的数据库中创建一条记录。
即每当我创建一本书时,都会在两个数据库(我的数据库和 API 的数据库上通过我的应用程序的 POST 请求)创建一条记录。
对于将使用 API 的应用程序,我正在使用 HTTParty gem,但请求仅在不影响 API 数据库的情况下运行
这是我的 HTTParty 发布请求代码
@result = HTTParty.post(' https://www.pingme.com/wp-json/wplms/v1/user/register',
:body => {
:books => {
:name => '#{name}',
:author => '#{author}',
:description => '#{description}',
:category_id => '#{category_id}',
:sub_category_id => '#{sub_category_id}'}.to_json,
:headers => { 'Content-Type' => 'application/json', 'Authorization' => '77d22458349303990334xxxxxxxxxx' })
但这不会影响 API 的数据库,而只会影响我的 Rails 应用程序的数据库
这是执行的日志代码
Started POST "/books" for 127.0.0.1 at 2019-03-27 11:51:18 +0100
Processing by BooksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxxxxx", "book"=>{"name"=>"veb", "author"=>"vebturejjd", "description"=>"aisiosoijjdkdp", "category_id"=>"text books", "sub_category_id"=>"children"}, "commit"=>"Create Book"}
(0.1ms) begin transaction
↳ app/controllers/books_controller.rb:32
Book Create (0.5ms) INSERT INTO "books" ("name", "author", "description", "category_id", "sub_category_id", "created_at", "updated_at", "client_id") VALUES (?, ?, ?, ?, ?, ?, ?) [["name", "vebturejjd"], ["author", "vebturejjd"], ["description", "aisiosoijjdkdp"], ["category_id", "text books"], ["sub_category_id", "children"], ["created_at", "2019-03-27 10:51:18.239045"], ["updated_at", "2019-03-27 10:51:18.239045"]]
↳ app/controllers/books_controller.rb:32
(77.8ms) commit transaction
我在终端中找不到@result 的任何日志,仍然想知道它是否被跳过或没有在运行时运行,或者有更好的方法。
我需要一些关于如何解析 ruby 以发布到 API 数据库的帮助。
这是我用于创建图书的图书控制器
require 'httparty'
class BooksController < ApplicationController
include HTTParty
before_action :set_book, only: [:show, :edit, :update, :destroy]
before_action :authenticate_admin!, except: %i[show index]
skip_before_action :verify_authenticity_token
# GET /books
# GET /books.json
def index
@books = Book.search(params[:keywords]).paginate(:page => params[:page], :per_page => 9).order('created_at DESC')
end
# GET /books/1
# GET /books/1.json
def show
end
# GET /books/new
def new
@book = Book.new
end
# GET /books/1/edit
def edit
end
# POST /books
# POST /books.json
def create
@book = Book.new(book_params)
respond_to do |format|
if @book.save
format.html { redirect_to @book, notice: 'Book was successfully created.' }
format.json { render :show, status: :created, location: @book }
else
format.html { render :new }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
@result = HTTParty.post(' https://www.pingme.com/wp-json/wplms/v1/user/register',
:body => {
:books => {
:name => '#{name}',
:author => '#{author}',
:description => '#{description}',
:category_id => '#{category_id}',
:sub_category_id => '#{sub_category_id}'}.to_json,
:headers => { 'Content-Type' => 'application/json', 'Authorization' => '77d22458349303990334xxxxxxxxxx' })
end
# PATCH/PUT /books/1
# PATCH/PUT /books/1.json
def update
respond_to do |format|
if @book.update(book_params)
format.html { redirect_to @book, notice: 'Book was successfully updated.' }
format.json { render :show, status: :ok, location: @book }
else
format.html { render :edit }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end
# DELETE /books/1
# DELETE /books/1.json
def destroy
@book.destroy
respond_to do |format|
format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_book
@book = Book.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def book_params
params.require(:book).permit(:name, :author, :description, :category_id, :sub_category_id)
end
end
我们将不胜感激任何形式的帮助。谢谢。
【问题讨论】:
-
您能记录下
@result是什么吗?另外,您有什么方法可以查看您发布到的 API 的日志,以查看是否收到了调用? -
如果您检查
@result变量的响应状态和正文,您可能会对正在发生的事情有更多的了解。 -
我不明白
@result是如何向您展示的。这些变量需要用双引号括起来,而不是单引号才能正确插值,即使这样,“#{name}”也应该是未定义的。 "#{@book.name}" 应该有一个值。此外,您永远不会传递真实性令牌,并且密钥是错误的“book”与“books”。 -
我已更新问题以明确显示 @result 的日志我目前无权查看我发布到的 API 日志。但是,每当我使用 Postman 测试 API 时,都会得到成功的响应。我需要你的帮助,我被困在这里。谢谢。
-
@oneWorkingHeadphone,感谢您指出这一点,我刚刚意识到整个日志代码仅用于 图书创建操作。对于这种误解,我深表歉意,我对 Ruby on Rails 还很陌生。我已经更新了这个问题。你知道任何更好的方法来解决这个问题,因为我找不到 result 的任何日志。谢谢。
标签: ruby-on-rails json ruby api httparty