benmccann/xero-java-client

Invoices not getting through.

Closed this issue · 3 comments

We had issues sending some Invoices through to Xero. The invoices that failed were returning "Error 17", something to do with a lack of data in the Invoice. The invoices in question had the '&' symbol in one of the lineItem description fields. Upon investigation we worked out that the Invoice XML payload did not appear to be getting encoded correctly in the OAuthRequest. On our system the default Charset for encoding was US-ASCII, but the Xero API documentation says it should be UTF-8. Also the Xero docs say the XML data should be attached as form data with the name "xml". Looking at the put method in XeroClient the XML is added add to the request via the addPayload method, which to me looks incorrect.

I altered the put method force the Charset on the request to be UTF-8 and replaced the addPayload call with addBodyParameter. This fixed the problem and I am now able to send Invoices.

Here is what the put method looks like now.

protected ResponseType put(String endPoint, JAXBElement<?> object) {
    OAuthRequest request = new OAuthRequest(Verb.PUT, BASE_URL + endPoint);
    String contents = marshallRequest(object);
    request.setCharset("UTF-8"); // set character set to UTF-8
    //request.addPayload(contents);
    request.addBodyParameter("xml", contents);
    service.signRequest(token, request);
    Response response = request.send();
    if (response.getCode() != 200) {
      throw newApiException(response);
    }
    return unmarshallResponse(response.getBody(), ResponseType.class);
}

You can send a pull request with this change and I can merge it.

Can you please include a link to the documentation page which says the XML data should be attached as form data with the name "xml"?

Thanks!

The bit about the xml data attachment is in the "HTTP POST and PUT" section of the "HTTP Requests and Responses" page in the "Getting Started" part of the API documentation.

http://developer.xero.com/documentation/getting-started/http-requests-and-responses/

Will do a pull request.

I think this was fixed by #12?