How to Implement This Post Request Using Httparty

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



Leave a reply



Submit