POST JSON for API using Rails and HTTParty

I would like a user in my ruby ​​on the rails application to be able to send a ticket to my external ticket management system squishlist.com. They have api and instructions as follows. You need to authenticate and get a token, and then send a ticket using 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 actions of the controllers and has the following code.

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

I know that it works in general because of the answers I received along the way. My json is different from the example due to the fields that I defined in squishlist. Can someone help me on this?

I guess the real problem is that I can't figure out what json looks like, and even if it is close to a match. I really don't really understand json. Should I use something that can be easy. Should I use ajax to post this. Any help is appreciated. I love the community here.

+81
json api ruby-on-rails
Sep 17 2018-11-17T00:
source share
2 answers

I solved this by adding .to_json and some header 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' } ) 
+195
Sep 30 2018-11-11T00:
source

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

 query_string_normalizer: ->(query){query.to_json} 
+10
Mar 06 '13 at 23:11
source



All Articles