Question

I'm trying to send a POST request to an api with a hash as the parameters of the request. The api in question is freshdesk's api. I'm using 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

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

Cheers! :)

Was it helpful?

Solution

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 ([{...).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top