[Ruby] Memorandum: What I was addicted to when tapping accounting freee API

less than 1 minute read

If you specify a query with GET, try http communication with uri.request_uri.

All this, it was just my mistake, not the accounting freee API specification.

I hit it with ruby

ACCESS_TOKEN has been acquired with OAuth 2.0. Set parameters like this

BASE_URL ='https://api.freee.co.jp'
MY_COMPANY_ID ='*********'
USE_ACCESS_TOKEN_HEADER = {
  "accept" => "application/json",
  "Authorization"=> "Bearer *************************"
}

When you hit the API to get the details of the customer as follows (It’s a simple method that is easy to understand)

def get_torihikisaki_id(name)
   uri = URI.parse(BASE_URL +'/api/1/partners')
   uri.query = URI.encode_www_form({"company_id" => MY_COMPANY_ID ,'keyword'=> name})
   http = Net::HTTP.new(uri.host, uri.port)
   http.use_ssl = true
   headers = USE_ACCESS_TOKEN_HEADER
   response = http.get(uri.path, headers)
   res_hash = JSON.parse(response.body)
end

puts get_torihikisaki_id ('hoge corporation')

I got a response like this.

{"status_code"=>400, "errors"=>[{"type"=>"validation", "messages"=>["company_id is not specified."]}]}

I have specified company_id ~ I wonder why I’ve been thinking a lot.

Cause

This sentence

response = http.get(uri.path, headers)

http communication was performed only with uri.path. I haven’t used it to set a query… Correctly

response = http.get(uri.request_uri, headers)

When embedding parameters in url with GET, it must be used properly…

Daily devotion