【问题标题】:POST JSON to API using Rails and HTTParty使用 Rails 和 HTTParty 将 JSON 发布到 API
【发布时间】:2011-11-19 07:45:02
【问题描述】:

我希望我的 ruby​​ on rails 应用程序中的用户能够向我的外部工单管理系统 squishlist.com 提交工单。他们有一个 api 和说明如下。您需要进行身份验证并获取令牌,然后使用令牌提交票证。来自 squishlist。

# get the token

https://api.squishlist.com/auth/?cfg=testcorp&user_key=privatekey&api_key=TEST-KEY-12345
  => {"token": "authtoken",
      "expires": "2010-06-16 13:31:56"}

# and then the ticket with the token

https://api.squishlist.com/rest/?cfg=testcorp&token=authtoken&method=squish.issue.submit&prj=demo
  POST data: {'issue_type': 1, 'subject': 'Hello, world.', 4: 'Open', 5: 10}

出于测试目的,我创建了一个控制器、路由和视图(页面)进行测试。在我的控制器上,我有以下

require 'httparty'
require 'json'

class SubmitticketController < ApplicationController

    def submit_a_ticket

        @cfg = 'xxxsupport'
        @user_key = '4787fsdbbfbfsdbhbfad5aba91129a3f1ed1b743321f7b'
        @api_key = 'MrUser411'
        @project = 'excelm-manoke'
        @url_new_string = 'https://api.squishlist.com/auth/?cfg='+@cfg+'&user_key='+@user_key+'&api_key='+@api_key
        # https://api.squishlist.com/auth/?cfg=xxxsupport&user_key=4787fsdbbfbfsdbhbfad5aba91129a3f1ed1b743321f7b&api_key=MrUser411  - this is what is created by @url_new_string
        response =  HTTParty.get(@url_new_string.to_str)  #submit the string to get the token
        @parsed_and_a_hash = JSON.parse(response)
        @token = @parsed_and_a_hash["token"]


        #make a new string with the token

        @urlstring_to_post = 'https://api.squishlist.com/rest/?cfg='+@cfg+'&token='+@token+'&method=squish.issue.submit&prj='+@project

        #submit and get a result

        @result = HTTParty.post(@urlstring_to_post.to_str, :body => {:subject => 'This is the screen name', :issue_type => 'Application Problem', :status => 'Open', :priority => 'Normal', :description => 'This is the description for the problem'})

    end

end

然后我有一个页面可以查看控制器操作的结果,其中包含以下代码。

<p><%= @result %></p>

我知道它总体上是有效的,因为我在此过程中收到了回复。由于我在 squishlist 中定义的字段,我的 json 与示例不同。谁能帮我解决这个问题?

我想真正的问题是我无法真正看到 json 的样子,以及它是否接近匹配。我真的对json不太了解。我是否应该使用一些可能很容易的东西。我应该使用 ajax 来提交这个吗?任何帮助是极大的赞赏。我喜欢这里的社区。

【问题讨论】:

    标签: ruby-on-rails json api httparty


    【解决方案1】:

    我通过添加.to_json 和一些标题信息解决了这个问题

    @result = HTTParty.post(@urlstring_to_post.to_str, 
        :body => { :subject => 'This is the screen name', 
                   :issue_type => 'Application Problem', 
                   :status => 'Open', 
                   :priority => 'Normal', 
                   :description => 'This is the description for the problem'
                 }.to_json,
        :headers => { 'Content-Type' => 'application/json' } )
    

    【讨论】:

    • 另外,像“GitLab API”这样的 API 对“Accept”标头是有意义的。所以标题应该是:headers =&gt; { 'Content-Type' =&gt; 'application/json', 'Accept' =&gt; 'application/json'}。注意:header不要转成JSON,应该是hash
    • 我部署了一个 Rails 引擎(打包为 gem),它对于在 Rails 上调试 API 非常有用。您只需安装引擎并转到您指定的 url,即“localhost:3000/api_explorer”即可查看它。这也是一种记录 API 的方式,从文件中读取 Web 服务规范。该 gem 名为“api_explorer”,repo 为github.com/toptierlabs/api_explorer 欢迎任何 cmets 或帮助改进 api。 :)
    • 很傻,文档中没有。
    • 谢谢!这很好用!不过,问题是:如何包含 JSON 数组?
    • 想像上述格式那样推送 90k 条记录之类的集合数据。我可以在单个 API 调用中推送整个数据吗?请让我知道你的cmets
    【解决方案2】:

    :query_string_normalizer 选项也可用,它将覆盖默认规范化器HashConversions.to_params(query)

    query_string_normalizer: ->(query){query.to_json}
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多