首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails:使用Rails和HTTParty将JSON发布到API

Rails:使用Rails和HTTParty将JSON发布到API
EN

Stack Overflow用户
提问于 2019-03-27 01:29:21
回答 2查看 704关注 0票数 0

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

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

也就是说,每当我创建一本书时,都要在两个数据库(我的数据库和API的数据库上,通过我的应用程序的POST请求)上创建一条记录。

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

对于将使用API的应用程序,我使用的是HTTParty gem

我尝试使用下面的代码在我的Books控制器的创建操作中实现:

代码语言:javascript
复制
 @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' })

这里的是我的图书控制器,用于创建图书

代码语言:javascript
复制
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

但是当我创建图书时,它仍然不会通过post请求在API的数据库上创建这些图书。

请提供任何形式的帮助,我们将非常感谢。谢谢你,

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-27 07:46:37

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

此处是更正后的HTTParty Post请求

代码语言:javascript
复制
@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'
  }
)

就这样

我希望这能帮上忙。

票数 0
EN

Stack Overflow用户

发布于 2019-03-27 02:22:28

在执行请求时检查日志,但我怀疑您需要将正文更改为:

代码语言:javascript
复制
{
  :book => {  
    :name => '#{name}',
    :author => '#{author}',
    :description => '#{description}',
    :category_id => '#{category_id}',
    :sub_category_id => '#{sub_category_id}'
  }
}.to_json

请注意,顶部的book键是不同之处。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55363033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档