2011-09-17 36 views
85

Tôi muốn cho người dùng trong ruby ​​của tôi trên ứng dụng đường ray có thể gửi vé tới hệ thống quản lý vé bên ngoài của tôi, squishlist.com. Họ có một api và hướng dẫn như sau. Bạn cần phải xác thực và nhận mã thông báo và sau đó gửi vé bằng mã thông báo. Từ squishlist.POST JSON tới API bằng cách sử dụng Rails và HTTParty

# 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} 

Để kiểm tra, tôi tạo bộ điều khiển, định tuyến và xem (trang) để thử nghiệm. Trên bộ điều khiển của tôi, tôi có:

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='[email protected]+'&user_key='[email protected]_key+'&api_key='[email protected]_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='[email protected]+'&token='[email protected]+'&method=squish.issue.submit&prj='[email protected] 

     #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 

Sau đó tôi có trang để xem kết quả của hành động bộ điều khiển và mã sau đây.

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

Tôi biết rằng nó đang hoạt động nói chung vì tôi đã nhận được phản hồi trong quá trình thực hiện. Json của tôi là khác nhau từ ví dụ vì các lĩnh vực tôi đã xác định trong squishlist. Bất cứ ai có thể giúp tôi về vấn đề này?

Tôi đoán vấn đề thực sự là tôi không thể thực sự nhìn thấy hình dạng của json và nếu nó gần như khớp với nhau. Tôi thực sự không biết nhiều về json. Tôi có nên sử dụng thứ gì đó có thể dễ dàng không. Tôi có nên sử dụng ajax để gửi nó không. Bất kỳ trợ giúp nào cũng được đánh giá rất cao. Tôi yêu cộng đồng ở đây.

Trả lời

200

tôi giải quyết điều này bằng cách thêm .to_json và một số thông tin hướng

@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' }) 
+6

Cảm ơn, người đàn ông. Điều đó .to_json trên phần tử cơ thể là phần tôi đã mất tích. – wndxlori

+8

Cảm ơn, người đàn ông. Các: tiêu đề chính là phần * I * đã mất tích. ;-) –

+0

Lưu ý 'as_json' không hoạt động. Nó cũng phải là 'to_json' – Hengjie

11

Tùy chọn :query_string_normalizer cũng có sẵn, mà sẽ ghi đè lên normalizer mặc định HashConversions.to_params(query)

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

Tuyệt vời! Điều này cho phép lưu trữ băm trong '' request.options [: body] '' nhưng gửi đúng chuỗi. Đây sẽ là câu trả lời thực sự cho câu hỏi. – freemanoid

+0

Tùy chọn này cũng có thể được đặt làm mặc định trong lớp bao gồm HTTParty bằng phương thức lớp query_string_normalizer, xem: http://www.ruby-doc.org/gems/docs/h/httparty2-0.7.10/HTTParty/ClassMethods .html # method-i-query_string_normalizer – Fryie

+0

Cũng có thể cần đặt tiêu đề kiểu nội dung: http: //www.ruby-doc.org/gems/docs/h/httparty2-0.7.10/HTTParty/ClassMethods.html # method-i-headers – Fryie

Các vấn đề liên quan