127

I would like for a user within my ruby on rails app to be able to submit a ticket to my external ticket management system, squishlist.com. They have an api and instructions as follows. You need to authenticate and get a token and then submit the ticket with the token. From squishlist.

# 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}

For testing purposes, I created a controller, route and view (page) for testing. On my controller I have the following

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

And then I have a page that I go to to see the result of the controllers actions and it has the following code.

<p><%= @result %></p>

I know that it is working in general because of responses I have received along the way. My json is different from the example because of fields I have defined in squishlist. Can anyone help me out on this issue?

I guess the real problem is that I can't really see what the json looks like and if it is even close to match. I really don't know much about json. Should I be using something that might be easy. Should I be using ajax to submit this. Any help is greatly appreciated. I love the community here.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Homer Jon
  • 4,463
  • 3
  • 15
  • 9

2 Answers2

307

I solved this by adding .to_json and some heading information

@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' } )
Community
  • 1
  • 1
Homer Jon
  • 4,463
  • 3
  • 15
  • 9
  • 12
    also, some APIs like the "GitLab API" is sensetive to "Accept" header. So the header should be `:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}`. Note: the header should not be converted to JSON, it is expected to be an hash – Devaroop Sep 28 '13 at 06:45
  • I deployed a Rails engine (packed as a gem) that is really useful to debug APIs on rails. You just have to mount the engine and go to the url that you specified, i.e. “localhost:3000/api_explorer” to see it. It’s a way of documenting an API also, reading the webservices specification from a file. The gem is named ‘api_explorer’ and the repo is http://www.github.com/toptierlabs/api_explorer Any comments or help improving the api is welcome. :) – Tony Nov 21 '13 at 19:13
  • 15
    It's just silly that that's not in the documentation. – Tyler Collier May 30 '14 at 16:00
  • Thanks! This worked great! Question, though: how would you include a JSON array? – Ruben Martinez Jr. Aug 13 '14 at 19:02
  • 1
    Want to push collection data like 90k records like in above format. Can I push entire data in single API call? please let me know your comments – Raju akula Apr 15 '15 at 10:47
  • 1
    The `.to_json` attached to `:body => {}.to_json` fixed my errors with this call. – user9869932 Nov 03 '21 at 00:44
  • the key is: `:body => {}.to_json`, missing this will lead to strange errors to server. – Siwei Sep 19 '22 at 08:06
14

The :query_string_normalizer option is also available, which will override the default normalizer HashConversions.to_params(query)

query_string_normalizer: ->(query){query.to_json}
Brian McNabb
  • 151
  • 1
  • 3
  • Awesome! This allows to store the hash in ``request.options[:body]`` but send the correct string. This should be the real answer to the question. – freemanoid Sep 19 '13 at 20:39
  • The option can also be set as a default in the class including HTTParty with the query_string_normalizer class method, see: http://www.ruby-doc.org/gems/docs/h/httparty2-0.7.10/HTTParty/ClassMethods.html#method-i-query_string_normalizer – Fryie Sep 24 '14 at 12:20
  • It may also be necessary to set the content-type header:http://www.ruby-doc.org/gems/docs/h/httparty2-0.7.10/HTTParty/ClassMethods.html#method-i-headers – Fryie Sep 24 '14 at 12:21
  • 1
    `query_string_normalizer` should be used for query strings, not post data. – josephrider May 31 '15 at 21:06
  • The links to `ruby-doc.org` are dead, the documentation is at [httparty doc](http://www.rubydoc.info/github/jnunemaker/httparty) – Yacc Oct 02 '15 at 01:39