का उपयोग कर एपीआई को पोस्ट JSON पोस्ट करें मैं अपने बाहरी टिकट प्रबंधन प्रणाली, squishlist.com पर टिकट जमा करने में सक्षम होने के लिए रेल ऐप पर अपने रूबी के भीतर एक उपयोगकर्ता के लिए चाहूंगा। उनके पास एक एपीआई और निर्देश हैं। आपको प्रमाणीकरण और टोकन प्राप्त करने की आवश्यकता है और फिर टोकन के साथ टिकट जमा करें। Squishlist से।रेल और 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}
परीक्षण उद्देश्यों के लिए, मैंने परीक्षण के लिए एक नियंत्रक, मार्ग और दृश्य (पृष्ठ) बनाया। मेरी नियंत्रक पर मैं निम्नलिखित
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
है और फिर मैं एक पृष्ठ है कि मैं नियंत्रक कार्यों के परिणाम को देखने के लिए जाने के लिए है और यह निम्नलिखित कोड है।
<p><%= @result %></p>
मुझे पता है कि यह क्योंकि प्रतिक्रियाओं मैं रास्ते में प्राप्त हुआ है की सामान्य रूप में काम कर रहा है। मेरा जेसन उदाहरण से अलग है क्योंकि मैंने स्कीशलिस्ट में परिभाषित फ़ील्ड की है। क्या कोई इस मुद्दे पर मेरी मदद कर सकता है?
मुझे लगता है कि असली समस्या यह है कि मैं वास्तव में नहीं देख सकता कि जेसन कैसा दिखता है और यदि यह मैच के करीब भी है। मैं वास्तव में जेसन के बारे में ज्यादा नहीं जानता। क्या मुझे ऐसा कुछ उपयोग करना चाहिए जो आसान हो। क्या मुझे इसे सबमिट करने के लिए AJAX का उपयोग करना चाहिए। कोई भी मदद बहुत ही सराहनीय होगी। मुझे यहां समुदाय से प्यार है।
धन्यवाद, आदमी। वह तत्व तत्व पर .to_json वह हिस्सा था जिसे मैं याद कर रहा था। – wndxlori
धन्यवाद, आदमी। : हेडर कुंजी भाग * I * गायब था। ;-) –
नोट 'as_json' काम नहीं करता है। इसे 'to_json' – Hengjie