How can I implement this POST request using HTTParty?
Just a guess, but it looks like you're passing a hash in the body when JSON is expected.
Try replacing the :body
declaration with:
:body => [{ "amount" => "0.25",
"platform" => "gittip",
"username" => "whit537" }].to_json
Edit:
I suggested the to_json
serializer, but misplaced it by putting it after the hash instead of the array and removing the array altogether. The example uses multiple records, so the array is necessary.
After looking at this thread, it looks like Gittip is picky about the accept header.
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
So, the full suggestion is:
HTTParty.post("https://www.gittip.com/#{user}/tips.json",
{
:body => [ { "amount" => "0.25", "platform" => "gittip", "username" => "whit537" } ].to_json,
:basic_auth => { :username => api_key },
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
})
How to format HTTParty POST request?
Just use HTTParty as a mixin instead in a class instead:
require 'httparty'
class MyApiClient
include HTTParty
base_uri 'https://app.api.com'
format :json
attr_accessor :api_key
def initalize(api_key:, **options)
@api_key = api_key
@options = options
end
def cc_query
self.class.post('/',
body: {
header: {
ver: 1,
src_sys_type: 2,
src_sys_name: 'Test',
api_version: 'V999'
},
command1: {
cmd: 'cc_query',
ref: 'test123',
uid: 'abc01',
dsn: 'abcdb612',
acct_id: 7777
}
},
query: {
api_key: api_key
}
)
end
end
Example usage:
MyApiClient.new(api_key: 'xxxxxxxx').cc_query
When you use format :json
HTTParty will automatically set the content type and handle JSON encoding and decoding. I'm guessing thats where you failed.
POST JSON to API using Rails and HTTParty
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' } )
HTTParty POST is sending a GET
Change your request to use HTTPS, not HTTP:
https://api.github.com/repos/my_github/my_repo/issues/1/comments
instead of:
http://api.github.com/repos/my_github/my_repo/issues/1/comments
and you get:
response.request
=> #<HTTParty::Request:0x00007ffdb81c7e48 @changed_hosts=false, @credentials_sent=false, @http_method=Net::HTTP::Post ...
as well as:
response.code
=> 201
Rails: Post JSON to API Using Rails and HTTParty
Following contributions from @paulo-fidalgo and @tejasbubane, I found a working solution to the issue.
Here is the corrected HTTParty Post Request
@results = HTTParty.post(' https://www.pingme.com/wp-json/wplms/v1/user/register',
:body => {
:name => "#{@book.name}",
:author => "#{@book.author}",
:description => "#{@book.description}",
:category_id => "#{@book.category_id}",
:sub_category_id => "#{@book.sub_category_id}"}.to_json,
:headers => {
'Content-Type' => 'application/json',
'Authorization' => '77d22458349303990334xxxxxxxxxx'
}
)
Related Topics
Rails Form Validation Conditional Bypass
Failing Installing Pg Gem, "Mkmf.Rb Can't Find Header Files for Ruby" (MAC Osx 10.6.5)
How to Use a Variable as Object Attribute in Rails
Converting from Xml Name-Values into Simple Hash
Activerecord::Connectiontimeouterror
Optional Arguments with Default Value in Ruby
Throw Exception When Re-Assigning a Constant in Ruby
How to Configure Ruby to Enter the Debugger on Ctrl-C (Sigint)
How to Pass Arguments to Define_Method
How to Ftp in Ruby Without First Saving the Text File
Why Doesn't "Rails S" Work from the App Directory
Generate an HTML Table from an Array of Hashes in Ruby
Ruby Modulo 3 with Negative Numbers Is Unintuitive
Ruby-Debug19 on Ruby-1.9.3-Preview1