GET requests send `data: {}`, which fetch-based runtimes reject

Open
#850 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
nodejs, typescript
Domain
api

Research direction

Start with the generated GET methods such as AccountingApi.getContact and getContacts, where request options are built with data: {}, and with the applyToRequest authentication hook the issue uses as a workaround. Check whether the fix belongs in the generated client or in the generator templates, since the issue does not say. Done means GET and HEAD requests omit data and a fetch-adapter test or a nock test passes for a GET call.

Written by the indexing model from the issue text.

Description

SDK you're using: xero-node 20.0.0 (same in 10.2.0 through 19.3.0), axios 1.19.0, Cloudflare Workers

Describe the bug

Every generated GET method (e.g. AccountingApi.getContact, getContacts, getInvoices) builds its request options with data: {}. axios serializes that to a "{}" body. Node's http adapter tolerates it, but on runtimes where axios uses its fetch adapter (Cloudflare Workers, and anywhere Request follows the Fetch spec), creating the Request throws:

TypeError: Request with a GET or HEAD method cannot have a body.

So no GET call works on Workers, even after the token request succeeds.

Expected behavior

Requests without a body parameter omit data (or set it to undefined).

Workaround

Replace the default authentication hook, which runs on each request just before axios sends it:

xero.accountingApi.setDefaultAuthentication({
  applyToRequest: (options) => {
    if (options.method === 'GET' || options.method === 'HEAD') delete options.data;
  },
});

Related: #737 (same cause, seen through nock), #740 (fetch adapter support).

Dominant language
TypeScript
Stars
264
Forks
177
Avg merge
10h 5m
Merged PRs (30d)
5

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from XeroAPI/xero-node

All issues in XeroAPI/xero-node

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.