Ruby on rails Rails:使用Rails和HTTParty将JSON发布到API

Ruby on rails Rails:使用Rails和HTTParty将JSON发布到API,ruby-on-rails,json,api,httparty,Ruby On Rails,Json,Api,Httparty,我对Rails中的API相当陌生,因此我需要一些帮助来解决我所面临的问题 我只想通过应用程序的POST请求在API数据库上创建一条记录 也就是说,每当我创建一本书时,通过应用程序的POST请求,在数据库my database和API数据库上创建一条记录 这就是我到目前为止所做的: 对于将使用API的应用程序,我正在使用 我已尝试使用以下代码在我的创建操作中实现Books Controller: @result = HTTParty.post(' https://www.pingme.com/w

我对Rails中的API相当陌生,因此我需要一些帮助来解决我所面临的问题

我只想通过应用程序的POST请求在API数据库上创建一条记录

也就是说,每当我创建一本书时,通过应用程序的POST请求,在数据库my database和API数据库上创建一条记录

这就是我到目前为止所做的:

对于将使用API的应用程序,我正在使用

我已尝试使用以下代码在我的创建操作中实现Books Controller:

 @result = HTTParty.post(' https://www.pingme.com/wp-json/wplms/v1/user/register', 
          :body => {  :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' })
这是我的图书控制器,用于创建图书

但是当我创建书籍时,它仍然没有通过post请求在API的数据库上创建这些书籍

请提供任何形式的帮助,我们将不胜感激。
谢谢。

当您提出请求时,请检查您的日志,但我怀疑您需要将您的身体更改为:

{ :book=>{ :name=>“{name}”, :author=>“{author}”, :description=>“{description}”, :category_id=>“{category_id}”, :sub_category_id=>“{sub_category_id}” } }.to_json
请注意,顶部的书键是区别。

在@paulo fidalgo和@tejasbubane的贡献之后,我找到了一个有效的解决方案

以下是更正的HTTParty Post请求

就这些


我希望这会有所帮助。

除了url中有额外的空间外,所有内容看起来都应该正常工作。放置一个调试器,查看post请求后的@result是什么。这可能会让你知道哪里出了问题。@evanbikes,谢谢你,我现在就去试试,看看它是否有效。@tejasbubane,我现在做这件事不是很有经验。我不知道是不是我在post请求主体中调用键值的方式导致了这些问题。或者你有更好的方法可以做到这一点吗?非常感谢你,让我现在就试试,虽然这很有效。非常感谢你。我非常感谢。不过我还有一些问题,请注意,问题是HttpParty post请求按照日志所示运行,但它没有将记录添加到API的数据库中。@PromisePreston可能会添加另一个问题并发布日志我刚刚添加了一个新问题,我也发布了日志。下面是问题的链接:请帮助我。非常感谢。
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 => {  :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
@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'
  }
)