使用Rails和HTTParty将JSON POST到API

Hom*_*Jon 104 api json ruby-on-rails httparty

我希望我的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}
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

然后我有一个页面,我去看看控制器操作的结果,它有以下代码.

<p><%= @result %></p>
Run Code Online (Sandbox Code Playgroud)

我知道它一般都是有效的,因为我一路上收到了回复.我的json与示例不同,因为我在squishlist中定义了字段.任何人都可以帮我解决这个问题吗?

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

Hom*_*Jon 244

我通过添加.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' } )
Run Code Online (Sandbox Code Playgroud)

  • 另外,像"GitLab API"这样的一些API对"Accept"标题是有意义的.所以标题应该是`:headers => {'Content-Type'=>'application/json','Accept'=>'application/json'}`.注意:标头不应该转换为JSON,它应该是一个哈希 (8认同)
  • 愚蠢的是,这不在文档中. (3认同)
  • 附加到 `:body =&gt; {}.to_json` 的 `.to_json` 修复了此调用中的错误。 (2认同)

小智 13

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

query_string_normalizer: ->(query){query.to_json}
Run Code Online (Sandbox Code Playgroud)