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'}
})
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' } )
Posting with httparty records and children records
Parent has_many Kids, then its expected to have an array here, try the following:
:kids => @parent.kids.map {|kid| {:school_name => kid.school.name, :age => kid.school.age }}
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
Related Topics
Link_To Method and Click Event in Rails
If Java People Go to Scala, C# Go to F#, Where Do Ruby People Go for Functional Nirvana
Convert String with Hex Ascii Codes to Characters
Ruby: Create a String from Bytes
Thread.Join Blocks the Main Thread
How to Pass a Custom Comparator to "Sort"
Why Does This Nokogiri Xpath Have a Null Return
How to Get the Intersection, Union, and Subset of Arrays in Ruby
Ruby Metaprogramming Online Tutorial
Installed Rails But the Rails Command Says It's Not Installed
Ruby Classes Within Classes (Or Modules Within Modules)