Rails:使用Rails和HTTParty将JSON发布到API无效 [英] Rails: Post JSON to API Using Rails and HTTParty Not Working
问题描述
尽管我已经获得了有关如何构造HTTParty Post Request的一些帮助,但是我对Rails中的API还是很陌生,但是我发送的有效负载(数据)不会影响我的API数据库
I am pretty new to API in Rails, although I have gotten some assistance already about how to structure my HTTParty Post Request, but the payload (data) that I am sending does not impact on the database of my API
我想要的是通过来自我的应用程序的POST请求在API的数据库上创建一条记录.
每当我创建一本书时,即在两个数据库(我的数据库和API的数据库中,通过来自我的应用程序的POST请求)上创建一条记录.
That is to create a record on both databases (my database and the on the database of the API through a POST request from my application) whenever I create a book.
对于将使用该API的应用程序,我正在使用HTTParty gem,但该请求仅在上运行,而不会影响该API的数据库
For the app that will consume the API I am using the HTTParty gem, but the request only runs on the without impacting the database of the 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应用程序的数据库
But this does not impact on the database of the API, rather it only impacts on the database of my Rails Application
这是执行的日志代码
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的任何日志,仍然想知道它是被跳过还是没有在运行时运行,或者有更好的方法.
I cannot find any log for @result in my terminal, still wondering if it was skipped or didn't run at run, or there is a better way to do it.
关于如何解析红宝石以发布到API数据库方面,我需要一些帮助.
Please I need some help on how to parse in ruby to be posted to the database of the 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
请提供任何形式的帮助,我们将不胜感激.谢谢.
推荐答案
在@ vincent-rolea和@oneWorkingHeadphone的贡献之后,我找到了解决该问题的有效方法.
Following contributions from @vincent-rolea and @oneWorkingHeadphone, I found a working solution to the issue.
这是对我有用的更正的HTTParty Post Request.
@results = HTTParty.post(' https://www.pingme.com/wp-json/wplms/v1/user/register',
:body => {
:name => "#{@book.name}",
:author => "#{@book.author}",
:description => "#{@book.description}",
:category_id => "#{@book.category_id}",
:sub_category_id => "#{@book.sub_category_id}"}.to_json,
:headers => {
'Content-Type' => 'application/json',
'Authorization' => '77d22458349303990334xxxxxxxxxx'
}
)
确保执行以下操作才能正常工作
- 在应用程序中安装并配置 HTTParty gem
- 在要执行请求的控制器中包含并要求 HTTParty gem li>
- 将 HTTParty gem 发布请求传递给该控制器中的实例变量
- Install and confgure the HTTParty gem in your application
- Include and require the HTTParty gem in the controller where you want the request to be performed
- Pass the HTTParty gem post request to an instance variable of in that controller
这是我控制器中的HTTParty Post Request的补充
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
@results = HTTParty.post(' https://www.pingme.com/wp-json/wplms/v1/user/register',
:body => {
:name => "#{@book.name}",
:author => "#{@book.author}",
:description => "#{@book.description}",
:category_id => "#{@book.category_id}",
:sub_category_id => "#{@book.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
仅此而已
我希望这会有所帮助.
这篇关于Rails:使用Rails和HTTParty将JSON发布到API无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!