【问题标题】:Post a hash using HTTParty to an api使用 HTTParty 将哈希发布到 api
【发布时间】:2014-05-06 05:02:25
【问题描述】:

我正在尝试将 POST 请求发送到带有哈希作为请求参数的 api。有问题的api是freshdesk的api。我正在使用 Rails 4。

他们要求我以这种形式发送请求:

代码

       {
          "helpdesk_ticket":{
              "description":"Some details on the issue ...",
              "subject":"Support needed..",
              "email":"hulk@outerspace.com",
              "priority":1, "status":2
          },
          "cc_emails":"superman@marvel.com,avengers@marvel.com"
        }

示例卷曲

curl -u user@yourcompany.com:test -H "Content-Type: application/json" -d '{ "helpdesk_ticket": { "description": "有关问题的详细信息...", "subject": "需要支持...", "email": “hulk@outerspace.com”、“优先级”:1、“状态”:2 }、“cc_emails”: “superman@marvel.com,avengers@marvel.com”}' -X POST http://domain.freshdesk.com/helpdesk/tickets.json

这是我目前拥有的

控制器

def create
    email = params[:email]
    description = params[:description]
    subject = params[:subject]
    payload = [{"helpdesk_ticket" => { 'email' => email, 'description' => description, 'subject' => subject, 'priority' => 1, 'status' => 2}}].to_json
    trololol = Freshdesk.post('/helpdesk/tickets.json',
                            :body => payload
                            )
        debugger
        redirect_to "/support"
    end


class Freshdesk
    include HTTParty
    @apikey = "Somethingladida"
    format :json
    base_uri "http://genericname.freshdesk.com/"
    basic_auth @apikey, ""
end

查看

=form_tag support_index_path do
    =label_tag :email
    =text_field_tag :email,params[:email]
    =label_tag :description
    =text_field_tag :description
    =label_tag :subject
    =text_field_tag :subject
    =submit_tag "Submit", class:"button"

主要问题是:我收到 500 内部服务器错误,所以我假设我尝试制作的请求不是freshdesk api 要求的格式

干杯! :)

【问题讨论】:

    标签: ruby-on-rails json api post request


    【解决方案1】:

    您可以通过执行以下操作来准确反映该结构:

    def freshdesk_hash
      {
        "helpdesk_ticket":{
            "description":"Some details on the issue ...",
            "subject":"Support needed..",
            "email":"hulk@outerspace.com",
            "priority":1, "status":2
          },
        "cc_emails":"superman@marvel.com,avengers@marvel.com"
      }
    end
    

    但在我看来,您将哈希放入数组 ([{...)。

    【讨论】:

    • 对不起阵列。那是我试图模仿 GET 调用对 api 的响应
    • 无论如何,我发现你的答案打破了轨道,但是一旦“helpdesk_ticket”周围的引号和其他键被删除,解决方案就完美了谢谢你的帮助^^
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多