How can I implement this POST request using HTTParty? - ruby

I am having difficulty making a POST request to an API endpoint using Ruby's HTTParty library. The API I'm interacting with is the Gittip API and their endpoint requires authentication. I have been able to successfully make an authenticated GET request using HTTParty.
You can see in the example code:
user = "gratitude_test"
api_key = "5962b93a-5bf7-4cb6-ae6f-aa4114c5e4f2"
# I have included real credentials since the above is merely a test account.
HTTParty.get("https://www.gittip.com/#{user}/tips.json",
{ :basic_auth => { :username => api_key } })
That request works and returns the following as expected:
[
{
"amount" => "1.00",
"platform" => "gittip",
"username" => "whit537"
},
{
"amount" => "0.25",
"platform" => "gittip",
"username" => "JohnKellyFerguson"
}
]
However, I have been unable to make a successful POST request using HTTParty. The Gittip API describes making a POST request using curl as follows:
curl https://www.gittip.com/foobar/tips.json \
-u API_KEY: \
-X POST \
-d'[{"username":"bazbuz", "platform":"gittip", "amount": "1.00"}]' \
-H"Content-Type: application/json"
I have tried (unsuccessfully) structuring my code using HTTParty as follows:
user = "gratitude_test"
api_key = "5962b93a-5bf7-4cb6-ae6f-aa4114c5e4f2"
HTTParty.post("https://www.gittip.com/#{user}/tips.json",
{
:body => [ { "amount" => "0.25", "platform" => "gittip", "username" => "whit537" } ],
:basic_auth => { :username => api_key },
:headers => { 'Content-Type' => 'application/json' }
})
The first argument is the url and the second argument is an options hash. When I run the code above, I get the following error:
NoMethodError: undefined method `bytesize' for [{"amount"=>"0.25", "platform"=>"gittip", "username"=>"whit537"}]:Array
from /Users/John/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http/generic_request.rb:179:in `send_request_with_body'
I have tried various other combinations of structuring the API call, but can't figure out how to get it to work. Here's another such example, where I do not user an array as part of the body and convert the contents to_json.
user = "gratitude_test"
api_key = "5962b93a-5bf7-4cb6-ae6f-aa4114c5e4f2"
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' }
})
Which returns the following (a 500 error):
<html>
<head>
<title>500 Internal Server Error</title>
</head>
<body>\n Internal server error, program!\n <pre></pre>
</body>
</html>
I'm not really familiar with curl, so I'm not sure if I am incorrectly translating things to HTTParty.
Any help would be appreciated. Thanks.

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'}
})

Related

jira-ruby: issue.save returning false

I am trying to create an issue with jira-ruby in the terminal. So far I have done the following (where username, password, site and project have been replaced with the proper values). I have been able to fetch issues, but not to create them. Jira-ruby return false when i try and save an issue
options = {
:username => "username",
:password => "password",
:site => 'site',
:context_path => '',
:auth_type => :basic
}
client = JIRA::Client.new(options)
issue = client.Issue.build
issue.save({
"fields" => {
"summary" => "blarg from in example.rb",
"project" => {"key" => "mykey"},
"issuetype" => {"id" => "1"}
}
})
=> false
issue.attrs
=> {"errorMessages"=>[], "errors"=>{"issuetype"=>"issue type is required"}, "summary"=>"blarg from in example.rb", "key"=>"somekey", "id"=>"someid", "self"=>"site", "exception"=>{"class"=>"Net::HTTPBadRequest", "code"=>"400", "message"=>"Bad Request"}}
What is the problem?

Can't get access token for ExactOnline with OAuth

I'm following the OAuth tutorial here to get a access code in order to authenticate my API requests to the online accounting software Exact Online.
However, I'm stuck at step 3, where I use the authorization code returned in step 2 to obtain the access token.
Here's what I'm trying:
require 'httparty'
EXACT_CLIENT_ID = '<REDACTED>'
EXACT_CLIENT_SECRET = '<REDACTED>'
EXACT_SERVER_BASE_URL = 'https://start.exactonline.nl'
EXACT_AUTH_CODE = '<REDACTED>'
response = HTTParty.post("#{EXACT_SERVER_BASE_URL}/api/oauth2/token", headers: {'Content-Type' => 'application/x-www-form-urlencoded'}, query: {code: EXACT_AUTH_CODE, redirect_uri: 'http://<REDACTED>.runscope.net/', grant_type: 'authorization_code', client_id: EXACT_CLIENT_ID, client_secret: EXACT_CLIENT_SECRET})
puts response
# => 'Bad request'
puts response.code
# => 400
I don't understand why this is happening. When looking at the list of response codes here, the code means:
The request could not be understood by the server due to malformed
syntax. The client SHOULD NOT repeat the request without
modifications.
What am I doing wrong?
Update #1:
I've also tried:
response_2 = HTTParty.post("#{EXACT_SERVER_BASE_URL}/api/oauth2/token", :headers => {'Content-Type' => 'application/x-www-form-urlencoded'}, :body => {'code' => EXACT_AUTH_CODE, 'redirect_uri' => 'http://<REDACED>.runscope.net/', 'grant_type' => 'authorization_code', 'client_id' => EXACT_CLIENT_ID, 'client_secret' => EXACT_CLIENT_SECRET})
But the response is the same.
Even though using the HTTP POST method you're providing the values as query parameters by supplying them to the query: parameter to the .post method. Instead you should provide them in the body: parameter, see: How can I implement this POST request using HTTParty?
Also your syntax re. body: :body should be fixed, so it would look like:
response_2 = HTTParty.post("#{EXACT_SERVER_BASE_URL}/api/oauth2/token", :headers => {'Content-Type' => 'application/x-www-form-urlencoded'}, :body => {'code' => EXACT_AUTH_CODE, 'redirect_uri' => 'http://<REDACED>.runscope.net/', 'grant_type' => 'authorization_code', 'client_id' => EXACT_CLIENT_ID, 'client_secret' => EXACT_CLIENT_SECRET})
Last but not least: the code value is one-time usage only and has a short lifetime; make sure you use a freshly obtained one.

Why am I getting this 'no implicit conversion of Hash into String' when mocking Stripe with WebMock

I'm trying to mock Stripe like this:
stub_request(:post, "https://api.stripe.com/v1/tokens").with(
:body => {"card" => {"number" => "4242424242424242", "exp_month"=> "12", "exp_year" => "2018", "cvc" => "123"}}
).
to_return(:status => 200, :headers => {}, :body => {
"id" => "tok_14CgP22eZvKYlo2CkgJ6ymLE",
"livemode" => false,
"created" => 1404517660,
"used" => false,
"object" => "token",
"type" => "card",
"card" => {
"id" => "card_14CgP22eZvKYlo2CcdUNvicW",
"object" => "card",
"last4" => "4242",
"brand" => "Visa",
"funding" => "credit",
"exp_month" => 12,
"exp_year" => 2018,
"fingerprint" => "Xt5EWLLDS7FJjR1c",
"country" => "US"
}
})
I am getting a no implicit conversion of Hash into String error when I call:
token = Stripe::Token.create(:card => {
:number => params["number"],
:exp_month => params["expiry_month"],
:exp_year => params["expiry_year"],
:cvc => params["cvc"]})
It's because of the hash in body in to_return, but I don't know how to avoid the error.
If I add .to_s to the hash, I get:
Invalid response object from API: "{\"id\"=>\"tok_14CgP22eZvKYlo2CkgJ6ymLE\", \"livemode\"=>false, \"created\"=>1404517660, \"used\"=>false, \"object\"=>\"token\", \"type\"=>\"card\", \"card\"=>{\"id\"=>\"card_14CgP22eZvKYlo2CcdUNvicW\", \"object\"=>\"card\", \"last4\"=>\"4242\", \"brand\"=>\"Visa\", \"funding\"=>\"credit\", \"exp_month\"=>12, \"exp_year\"=>2018, \"fingerprint\"=>\"Xt5EWLLDS7FJjR1c\", \"country\"=>\"US\"}}" (HTTP response code was 200)
If I add a .to_json to the end, Stripe::Token passes but I get a stack error too deep error when I try to use the resulting token in any way.
What do you recommend?
It could be that your stubbing is not returning correctly. It may help to make your stubbing a little less specific. For example:
body = {
"id" => "tok_14CgP22eZvKYlo2CkgJ6ymLE",
"livemode" => false
# snip...
}
stub_request(:post, "https://api.stripe.com/v1/tokens")
.to_return(:status => 200, :body => body.to_json)
However, it's difficult to know exactly what errors are being thrown, so I can only give vague ideas which may help. What error does calling token give you?

How to avoid uri encoding on JSON when posting from Ruby?

I am trying to write a simple http post which will send a json body to a restful api. I am using the following code:
data = {'EmailAddress' => email, 'Name' => 'bobby', 'CustomFields' => [ {'Key' => 'Country', 'Value' => 'canada'}, {'Key' => 'City', 'Value' => 'vancouver'} ], 'Resubscribe' => true }.to_json
require 'net/http'
net = Net::HTTP.new("api.createsend.com", 80)
request = Net::HTTP::Post.new("/api/v3/subscribers/#{#list}.json")
request.basic_auth(#api, 'magic')
request.set_form_data(:body => data)
response = net.start do |http|
http.request(request)
end
puts response.code
puts response.read_body
The trouble I am having is the body is going to the server as a string and not as hex. Here is what I am sending:
body=%7b%22EmailAddress%22%3a%223%40blah.com%22%2c%22Name%22%3a%22bobby%22%2c%22CustomFields%22%3a%5b%7b%22Key%22%3a%22Country%22%2c%22Value%22%3a%22canada%22%7d%2c%7b%22Key%22%3a%22City%22%2c%22Value%22%3a%22vancouver%22%7d%5d%2c%22Resubscribe%22%3atrue%7d
Here is what I want to send:
{
"EmailAddress": "3#blah.com",
"Name": "bobby",
"CustomFields" : [
{
"Key":"Country",
"Value":"canada"
},
{
"Key":"City",
"Value":"vancouver"
}
],
"Resubscribe": true
}
How can I pack this data so it is not going out as a string?
Instead of:
request.set_form_data(:body => data)
Try it like this:
request.body = data
net/http should not be uri-encoding the post body, if you see that happening then maybe something else along the way is doing it or maybe you're just mistaken.

Ruby rest-client file upload as multipart form data with basic authenticaion

I understand how to make an http request using basic authentication with Ruby's rest-client
response = RestClient::Request.new(:method => :get, :url => #base_url + path, :user => #sid, :password => #token).execute
and how to post a file as multipart form data
RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb')
but I can't seem to figure out how to combine the two in order to post a file to a server which requires basic authentication. Does anyone know what is the best way to create this request?
How about using a RestClient::Payload with RestClient::Request...
For an example:
request = RestClient::Request.new(
:method => :post,
:url => '/data',
:user => #sid,
:password => #token,
:payload => {
:multipart => true,
:file => File.new("/path/to/image.jpg", 'rb')
})
response = request.execute
RestClient API seems to have changed. Here's the latest way to upload a file using basic auth:
response = RestClient::Request.execute(
method: :post,
url: url,
user: 'username',
password: 'password',
timeout: 600, # Optional
payload: {
multipart: true,
file: File.new('/path/to/file, 'rb')
}
)
The newest best way may be that:
the link is enter link description here
RestClient.post( url,
{
:transfer => {
:path => '/foo/bar',
:owner => 'that_guy',
:group => 'those_guys'
},
:upload => {
:file => File.new(path, 'rb')
}
})
Here is an example with a file and some json data:
require 'rest-client'
payload = {
:multipart => true,
:file => File.new('/path/to/file', 'rb'),
:data => {foo: {bar: true}}.to_json
}
r = RestClient.post(url, payload, :authorization => token)

Resources