首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Rails和HTTParty将JSON发布到API

使用Rails和HTTParty将JSON发布到API
EN

Stack Overflow用户
提问于 2011-09-17 23:12:43
回答 2查看 118.6K关注 0票数 114

我希望我的ruby on rails应用程序中的用户能够向我的外部票证管理系统squishlist.com提交票证。它们有一个api和如下说明。您需要进行身份验证并获取令牌,然后使用令牌提交票证。来自squishlist。

代码语言:javascript
复制
# 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}

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

代码语言:javascript
复制
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

然后我有一个页面,我转到该页面查看控制器操作的结果,它包含以下代码。

代码语言:javascript
复制
<p><%= @result %></p>

我知道它在总体上是有效的,因为我在这个过程中收到了一些反馈。我的json与示例不同,因为我在squishlist中定义了字段。在这个问题上,有人能帮我吗?

我想真正的问题是我看不到json是什么样子的,它是否接近匹配。我真的不太了解json。我是否应该使用一些可能简单的东西。我应该使用ajax来提交这篇文章吗?任何帮助都是非常感谢的。我喜欢这里的社区。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-01 07:57:58

我通过添加.to_json和一些标题信息解决了这个问题

代码语言:javascript
复制
@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' } )
票数 283
EN

Stack Overflow用户

发布于 2013-03-07 07:11:03

还可以使用:query_string_normalizer选项,该选项将覆盖缺省的规格化器HashConversions.to_params(query)

代码语言:javascript
复制
query_string_normalizer: ->(query){query.to_json}
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7455744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档