title: Rails: Post JSON to API Using Rails and HTTParty
body: <p>I'm fairly new to API in Rails, and so I will need some assistance for the issue that I am facing.</p>
  
  <p><strong>All I want is to create a record on the database of the API through a POST request from my application.</strong></p>
  
  <p>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.</p>
  
  <p>So this is what I've done so far:</p>
  
  <p>For the app that will consume the API I am using the <a href="https://github.com/jnunemaker/httparty" rel="nofollow noreferrer">HTTParty gem</a>.</p>
  
  <p>I have tried to implement in my create action of the Books Controller using the code below:</p>
  
  <pre><code> @result = HTTParty.post(' https://www.pingme.com/wp-json/wplms/v1/user/register', 
            :body =&gt; {  :name =&gt; '#{name}',
                        :author =&gt; '#{author}',
                        :description =&gt; '#{description}',
                        :category_id =&gt; '#{category_id}',
                        :sub_category_id =&gt; '#{sub_category_id}'}.to_json, 
            :headers =&gt; { 'Content-Type' =&gt; 'application/json', 'Authorization' =&gt; '77d22458349303990334xxxxxxxxxx' })
  </code></pre>
  
  <p><strong>Here is my Books Controller for creating books</strong></p>
  
  <pre><code>require 'httparty'
  
  class BooksController &lt; 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 =&gt; params[:page], :per_page =&gt; 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 =&gt; {  :name =&gt; '#{name}',
                    :author =&gt; '#{author}',
                    :description =&gt; '#{description}',
                    :category_id =&gt; '#{category_id}',
                    :sub_category_id =&gt; '#{sub_category_id}'}.to_json, 
        :headers =&gt; { 'Content-Type' =&gt; 'application/json', 'Authorization' =&gt; '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
  </code></pre>
  
  <p>But it still doesn't create these books on the database of the API through the post request when I create books.</p>
  
  <p><strong>Please any form of assistance will be highly appreciated.
  Thank you.</strong></p>