복붙노트

[RUBY-ON-RAILS] 레일 및 사용하여 API에 POST JSON HTTParty

RUBY-ON-RAILS

레일 및 사용하여 API에 POST JSON HTTParty

내 외부 티켓 관리 시스템, 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>

나는 응답의 난 길을 따라받은 있기 때문에 일반적으로 작동하는지 알고있다. 내 JSON은 예로부터 때문에 내가 squishlist에서 정의한 필드 다르다. 이 문제에 캔 사람의 도움이 나를?

나는 진짜 문제는 정말 무슨 JSON의 모습처럼하고 근처에도 일치하는 경우 볼 수없는 것 같다. 정말 많은 JSON에 대해 알고하지 않습니다. 나는 쉬울 수도 것을 사용한다. 나는이 제출 아약스를 사용해야합니다. 어떤 도움도 대단히 감사합니다. 나는 여기에 지역 사회를 사랑 해요.

해결법

  1. ==============================

    1.나는 .to_json를 추가하여이 문제를 해결하고 몇 가지 제목 정보

    나는 .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' } )
    
  2. ==============================

    2.: query_string_normalizer 옵션은 HashConversions.to_params 노말 기본 (쿼리)를 오버라이드 (override) 할도 사용할 수있다

    : query_string_normalizer 옵션은 HashConversions.to_params 노말 기본 (쿼리)를 오버라이드 (override) 할도 사용할 수있다

    query_string_normalizer: ->(query){query.to_json}
    
  3. from https://stackoverflow.com/questions/7455744/post-json-to-api-using-rails-and-httparty by cc-by-sa and MIT license