Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong Content-Type in 4.1.0 #86

Closed
duk3luk3 opened this issue Jan 8, 2018 · 7 comments
Closed

Wrong Content-Type in 4.1.0 #86

duk3luk3 opened this issue Jan 8, 2018 · 7 comments
Assignees
Labels

Comments

@duk3luk3
Copy link
Contributor

duk3luk3 commented Jan 8, 2018

I just upgraded from 3.1.4 to 4.1.0.

Here's how I initialize kitsu:

<script>
const Kitsu = require('kitsu');

const api = new Kitsu({
    baseURL: '{{config['PREFERRED_URL_SCHEME']}}://{{config['SERVER_NAME']}}/api',
//    headers: {
//        Authorization: 'foobar'
//    }
});
api.headers['Authorization'] = 'foobar';

And here's how I make a request:

        api.update('containers', {
            id: container_id,
            status: container_action
        }).then(function(res) {
            console.log(res);
        })
            .catch((reason) => console.log(reason));

My requests should be sent with application/vnd.api+json Content-Type, but they aren't, no matter what I do it is sent with application/json;charset=utf-8 which happens to be the Axios default.

So I suspect that something broke between 3.x and 4.x that causes Axios to no longer set / see the correct Content-Type.

Here is the libs I am using:

├─ [email protected]
│  ├─ axios@^0.17.0
│  ├─ babel-runtime@^6.26.0
│  └─ pluralize@^7.0.0
@wopian
Copy link
Owner

wopian commented Jan 8, 2018

I can't seem to replicate this - request headers correctly contained application/vnd.api+json for everything I've tried.

Only change is that axios is now just imported and used directly instead of being added to the class and used internally. This shouldn't have any effect on the headers though - the Accept and Content-Type headers are added over everything else.


Also you'll want to add resourceCase: 'snake' to the initialize options - the default changed from snake_case to kebab-case in 4.x

@wopian wopian added the bug label Jan 8, 2018
@duk3luk3
Copy link
Contributor Author

duk3luk3 commented Jan 8, 2018

Hmm, I'll debug into this more once I am less sleep-deprived.

Is there a way to get the non-minified versions of axios and kitsu browserified with all its dependencies so I can debug this in my application?

@wopian
Copy link
Owner

wopian commented Jan 8, 2018

Actually - the headers were changed from content-type to Content-Type in 4.x and is dropping the content type header, but not the accept header (I may have been looking at the responses' content type header last night 😆). May be a bug in Axios though. axios/axios#86 axios/axios#89

Don't have the time to do anything until the weekend, but reverting back to creating a new instance of Axios in the class constructor may fix this.

You can use Browserify to bundle the dependencies into a single file.

@duk3luk3
Copy link
Contributor Author

duk3luk3 commented Jan 8, 2018

You can use Browserify to bundle the dependencies into a single file.

Yes, but how do I do that with the non-minified versions? My npm-fu is weak.

I tried this in a source checkout:

yarn
yarn add browserify
yarn run browserify src/ -o src_bundle.js

It runs into this:

yarn run browserify src -o src_bundle.js                                                                                                                                         ~/git/personal/kitsu
yarn run v1.3.2
$ /home/erlacher/git/personal/kitsu/node_modules/.bin/browserify src -o src_bundle.js
Error: Parsing file /home/erlacher/git/personal/kitsu/src/index.js: Unexpected token (292:8)
    at Deps.parseDeps (/home/erlacher/git/personal/kitsu/node_modules/module-deps/index.js:481:28)
    at getDeps (/home/erlacher/git/personal/kitsu/node_modules/module-deps/index.js:414:40)
    at /home/erlacher/git/personal/kitsu/node_modules/module-deps/index.js:398:32
    at ConcatStream.<anonymous> (/home/erlacher/git/personal/kitsu/node_modules/concat-stream/index.js:36:43)
    at ConcatStream.emit (events.js:164:20)
    at finishMaybe (/home/erlacher/git/personal/kitsu/node_modules/readable-stream/lib/_stream_writable.js:607:14)
    at endWritable (/home/erlacher/git/personal/kitsu/node_modules/readable-stream/lib/_stream_writable.js:615:3)
    at ConcatStream.Writable.end (/home/erlacher/git/personal/kitsu/node_modules/readable-stream/lib/_stream_writable.js:571:41)
    at DuplexWrapper.onend (/home/erlacher/git/personal/kitsu/node_modules/readable-stream/lib/_stream_readable.js:570:10)
    at Object.onceWrapper (events.js:254:19)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

@wopian
Copy link
Owner

wopian commented Jan 10, 2018

You'll need to target one of the lib files as Browserify doesn't like ES6 imports - run yarn build then yarn run browserify lib/kitsu.js -o src_bundle.js

@duk3luk3
Copy link
Contributor Author

OK, yep, that works, I have no idea how I managed not to try that variant during my attempts at randomly guessing how to get it right...

So I think I've figured out the problem - could have figured that out just by staring harder at the code instead of spending hours making it debuggable, but it appears that the axios api actually changed:

https://github.com/axios/axios/blob/master/README.md#axiospatchurl-data-config

axios.patch(url[, data[, config]])

kitsu does this:

kitsu/src/index.js

Lines 180 to 183 in 6b0ee2b

const { data } = await axios.patch(url, {
data: (await serialise.apply(this, [ model, body, 'PATCH' ])).data,
headers
})

But should do this:

      const { data } = await axios.patch(url,
        {data: (await serialise.apply(this, [ model, body, 'PATCH' ])).data},
        {headers: headers}
      )

In my browserified js, this is what the working code looks like:

        var _ref2 = yield axios.patch(url,
            {data: (yield serialise.apply(_this2, [model, body, 'PATCH'])).data},
          {headers: headers}
        );

        const data = _ref2.data;

If that analysis makes sense to you, I'll be happy to make a PR.

duk3luk3 added a commit to duk3luk3/kitsu that referenced this issue Jan 12, 2018
The axios api apparently changed and we weren't looking.

Headers are now passed to axios in separate "config" parameter dict
instead of in the "data" dict.

Fixes wopian#86
@ghost ghost assigned wopian Jan 13, 2018
@wopian
Copy link
Owner

wopian commented Jan 16, 2018

Fixed in 4.2.0

@wopian wopian closed this as completed Jan 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants