115

Ruby on Rails アプリ内のユーザーが、外部のチケット管理システムである squishlist.com にチケットを送信できるようにしたいと考えています。次のような API と手順があります。認証してトークンを取得してから、トークンを使用してチケットを送信する必要があります。スキッシュリストより。

# 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を使用する必要がありますか?どんな助けでも大歓迎です。ここのコミュニティが大好きです。

4

2 に答える 2

288

.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' } )
于 2011-09-30T23:57:58.160 に答える
14

デフォルトの:query_string_normalizerノーマライザーをオーバーライドするオプションも利用できます。HashConversions.to_params(query)

query_string_normalizer: ->(query){query.to_json}
于 2013-03-06T23:11:03.417 に答える