Vue-resource: support lowercase response content-type

Created on 4 Jul 2016  路  23Comments  路  Source: pagekit/vue-resource

https://github.com/vuejs/vue-resource/blob/master/src/http/interceptor/body.js#L25


    next((response) => {

        var contentType = response.headers['Content-Type'];

        if (isString(contentType) && contentType.indexOf('application/json') === 0) {
Bug

Most helpful comment

Since HTTP header field names are case-insensitive, maybe it would be best to lowercase all the keys of Response.headers object? If this breaks for anyone, then their code is non-standard.

All 23 comments

Came here to report the very same bug. I call that coincidence!

I can confirm this bug. In Chrome, response won't be transformed to json because response.headers['Content-Type'] = null.
response.headers['content-type'] = application/json.

In the meantime, you can solve it like this:

https://github.com/vuejs/vue-resource/issues/314

Since HTTP header field names are case-insensitive, maybe it would be best to lowercase all the keys of Response.headers object? If this breaks for anyone, then their code is non-standard.

We found this bug on latest Chrome, json data be resolved as string not object.

response.json()

@LinusBorg
Can you elaborate what you mean with your response? In terms of monkey-patching the issue, @Jigsaw5279 has already provided a more versatile and useful solution.

Ignore my comment, I had a misunderstanding.

Hey Guys,

I'm having the same issue. I wrote a simple interceptor to fix it for the time being if this helps anybody:

Vue.http.interceptors.push((request, next ) => {
    next((response) => {
        if( 'Content-Type' in response.headers 
            && response.headers['Content-Type'] == 'application/json' ){
            if( typeof response.data != 'object' ){
                response.data = JSON.parse( response.data );
            }
        }

        if( 'content-type' in response.headers
            && response.headers['content-type'] == 'application/json' ){
            if( typeof response.data != 'object' ){
                response.data = JSON.parse( response.data );
            }
        }
    });
});

This too has had me stumped for days! My scripts worked fine via plain Apache but broke when served via CloudFlare, and only on Chrome!

Fix would be much appreciated

Indeed this is broken.

It's already been fixed in the develop branch with this commit

the next release will solve this.

Thanks @LinusBorg!

@steffans fixed it, not me ;)

@LinusBorg thanks for letting us know ;). @steffans thanks for fixing 馃憤

Is this fixed ? Because i face same problem in [email protected]

Since there was no new release since my last comment:

No, not in a public release (but yes, in the develop branch)

Hi, how do i install vue-resource develop branch using npm?

Thanks!

you can't. npm does not have the whole repository, only published versions.

you can however clone the repo as a subModule into a folder of your projects repo, checkout the develop branch, build it, and require it from there.

of course, do that at your own risk, the development branch may have bugs, or not even be working at all.

Alternatively/to improve this, you could use npm link to link to a local copy of the vue-resource repo.

Then you can still do require('vue-resource'), as npm_modules/vue-resource will be symlinked to the local repo of vue-resource

@uxweb @LinusBorg
In fact, you can!

  1. Just navigate to the commit you want, for example this one
  2. Copy the url and replace commit with tarball
  3. Install it normally via npm, for example npm i -D https://github.com/vuejs/vue-resource/tarball/91fefa21faa88bc98271b43adbc23384ba46b93f

Beware: This doesn't work for every module. Just for those that have no complicated npm publish setup and basically push everything from github to npm. But for vue-resource this works.

Beware 2: It's not recommended to use dev branches or random commits for anything more important than trying out stuff or hobby projects. There's a big chance that such intermediary states have errors and/or flaws. After all, there are npm releases for a reason.

@LinusBorg Thanks!!,

I think is a better idea to include the interceptor proposed by @Jigsaw5279 馃憣

Interceptor worked great, thanks! 馃憤

FIY, just saying

Since HTTP header field names are case-insensitive, maybe it would be best to lowercase all the keys of Response.headers object? If this breaks for anyone, then their code is non-standard.

Is very reckless. There are frameworks out there (e.g. Code Igniter) that do not accept e.g. authorization. This will have indeed broken front-ends all around. Great. 馃憥

Was this page helpful?
0 / 5 - 0 ratings

Related issues

odranoelBR picture odranoelBR  路  6Comments

Creabine picture Creabine  路  3Comments

santigraviano picture santigraviano  路  5Comments

laizhenhai88 picture laizhenhai88  路  4Comments

ayyobro picture ayyobro  路  3Comments