简体   繁体   English

使用HTTParty将哈希值发布到api

[英]Post a hash using HTTParty to an api

I'm trying to send a POST request to an api with a hash as the parameters of the request. 我正在尝试将POST请求发送到带有哈希作为请求参数的api。 The api in question is freshdesk's api. 有问题的api是freshdesk的api。 I'm using rails 4. 我正在使用Rails 4。

They require that i send a request in this form: 他们要求我以这种形式发送请求:

Code

       {
          "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"
        }

Sample Curl 样品卷曲

curl -u user@yourcompany.com:test -H "Content-Type: application/json" -d '{ "helpdesk_ticket": { "description": "Details about the issue...", "subject": "Support Needed...", "email": "hulk@outerspace.com", "priority": 1, "status": 2 }, "cc_emails": "superman@marvel.com,avengers@marvel.com" }' -X POST http://domain.freshdesk.com/helpdesk/tickets.json curl -u user@yourcompany.com:test -H“内容类型:application / json” -d'{“ helpdesk_ticket”:{“ description”:“有关此问题的详细信息...”,“ subject”:“支持需要...“,”电子邮件“:” hulk@outerspace.com“,”优先级“:1,”状态“:2},” cc_emails“:” superman @ marvel.com,avengers @ marvel.com“}' -X POST http://domain.freshdesk.com/helpdesk/tickets.json

This is currently what I have 这是我目前所拥有的

Controller 控制者

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

View 视图

=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"

The main problem is this: I'm getting a 500 internal server error, so I'm presuming that the request that i'm trying to craft is not in the format that the freshdesk api requires 主要问题是:我收到500个内部服务器错误,因此我假设我尝试制作的请求的格式不符合freshdesk API要求的格式

Cheers! 干杯! :) :)

You can mirror that structure exactly by doing something like this: 您可以通过执行以下操作来完全镜像该结构:

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

But to me it looks like you're putting the hash into an array ( [{... ). 但是对我来说,您似乎将哈希值放入了数组( [{... )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM