Created
January 28, 2016 14:08
-
-
Save x4d3/2583690ee3047d13402a to your computer and use it in GitHub Desktop.
Gist showing an issue with Xero API that returns xml instead of Json when there is a validation error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'bundler/setup' | |
require 'xero_gateway' | |
YOUR_OAUTH_CONSUMER_KEY = '' | |
YOUR_OAUTH_CONSUMER_SECRET = '' | |
PATH_TO_YOUR_PRIVATE_KEY = 'privatekey.pem' | |
url = "https://api.xero.com/payroll.xro/1.0/Employees" | |
options = { | |
:signature_method => 'RSA-SHA1', | |
:private_key_file => PATH_TO_YOUR_PRIVATE_KEY | |
} | |
client = XeroGateway::OAuth.new(YOUR_OAUTH_CONSUMER_KEY, YOUR_OAUTH_CONSUMER_SECRET, options) | |
client.authorize_from_access(YOUR_OAUTH_CONSUMER_KEY, YOUR_OAUTH_CONSUMER_SECRET) | |
headers = {'charset' => 'utf-8'} | |
headers['Content-Type'] ||= "application/x-www-form-urlencoded" | |
# we are asking for JSON | |
headers['Accept'] ||= 'application/json' | |
uri = URI.parse(url) | |
b = Builder::XmlMarkup.new | |
body = b.Employees { | |
b.Employee { | |
b.LastName SecureRandom.uuid | |
b.FirstName SecureRandom.uuid | |
b.WorkLocations 'super wrong' | |
} | |
b.Employee { | |
b.LastName SecureRandom.uuid | |
b.FirstName SecureRandom.uuid | |
b.WorkLocations 'super wrong' | |
} | |
} | |
response = client.put(uri.request_uri, { :xml => body }, headers) | |
result = response.plain_body.force_encoding("UTF-8") | |
p result | |
# this fail because result is not JSON but xml | |
# "<Response xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><ErrorNumber>0</ErrorNumber><Type>Error</Type><Message>WorkLocations is not a valid element in Employee</Message></Response>" | |
# parsed = JSON.parse result | |
# p parsed | |
# p parsed["ValidationErrors"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment