SpoonX/aurelia-api

POST/PUT requests are not properly sending json body

Closed this issue · 6 comments

My code worked in 3.1.1 and earlier, but after updating to 3.2.0 latest, all POST and PUT requests in my application are broken.

The request payload is coming across as "[object Object]" instead of the expected json stringified object. Request header is getting this set to "Content-Type: text/plain;charset=UTF-8", expected it to auto-set it to "application/json" as it did before.

Reverted API (and ORM and Authentication dependencies) to 3.1.1, and those same calls are coming back with the json string in body and 'application/json' as expected.

Defaults mentioned in docs aren't being applied. If I set those default values in api initial configuration, it corrects this issue.
.withDefaults({headers: {'Content-Type':'application/json', 'Accept':'application/json'}})

I output this.defaults in the request() function in aurelia-api.js and it was an empty object. Not sure how but this.defaults is going from being set in constructor to being empty object by the time I hit request().

@meash-nrel That's unfortunate, I'm sorry you're having trouble with this.

Having figured out the issue, do you feel confident submitting a PR?

Caused by #215. @netcitylife accurately identified the issue, but was ignored :(

30dbbc5#diff-9d9a6cd82f41984872a66a3ab0d440c4R74

On the linked line, the newClient.defaults property is the empty object when it is unconfigured - not null. Then it goes on and overrides the aurelia-api defaults with the empty object.

Workaround is to set the defaults to a falsey value in the config.registerEndpoint() function:

plugin(
    'aurelia-api',
    config => {
      config.registerEndpoint(
          'github',
          endpoint => endpoint.withDefaults(undefined)
      )
    }
);

@carusology Nice find. Maybe you want to contribute a more sustainable fix? It'd be much appreciated!

@RWOverdijk
Is there documentation on how to run the tests anywhere?

npm install and npm test do not appear to be sufficient. The contributing guidelines reference running gulp globally, but that is what npm test is doing.

@carusology I'm pretty sure that just running yarn test (or npm) should just work if you run jspm install first.