SpoonX / aurelia-api

A simple convenience wrapper around aurelia-fetch-client
http://aurelia-api.spoonx.org
MIT License
72 stars 21 forks source link

POST/PUT requests are not properly sending json body #222

Closed meash-nrel closed 6 years ago

meash-nrel commented 6 years ago

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.

meash-nrel commented 6 years ago

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().

RWOverdijk commented 6 years ago

@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?

carusology commented 6 years ago

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

https://github.com/SpoonX/aurelia-api/pull/215/commits/30dbbc58476582ff1544531772f3795ceac95050#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)
      )
    }
);
RWOverdijk commented 6 years ago

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

carusology commented 6 years ago

@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.

RWOverdijk commented 6 years ago

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