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

Duplicated content-type on vanilla gateway #32

Closed
ericholiveira opened this issue Oct 11, 2016 · 3 comments
Closed

Duplicated content-type on vanilla gateway #32

ericholiveira opened this issue Oct 11, 2016 · 3 comments
Labels

Comments

@ericholiveira
Copy link

Im trying to do a POST to my server i configured my gateway as follows:

import Mappersmith from 'mappersmith';
Mappersmith.Env.USE_PROMISES = true;
var manifest = {
 host: 'http://localhost:3000',
 rules: [
    {
        values: {
            gateway: {
                headers: {
                    'Content-Type': 'application/json'
                }
            }
        }
    }
 ],
 resources: {
   CreateMixData: {
     search: 'post:/search',
     data: '/data'
   }
 }
};
export default Mappersmith.forge(manifest);```

and im calling it as:
```js
API.CreateMixData.search({body:JSON.stringify({category:21})})

The problem is, when i look at the call on chrome the content-type is "Content-Type:application/json, application/json" instead of "Content-Type:application/json" , and for this reason my server dont know how to parse it.

The problem happens on any verb except for GET , and i tracked down the problem to the following method on vanilla-gateway:

_performRequest: function(method) {
    var emulateHTTP = this.shouldEmulateHTTP(method);
    var requestMethod = method;
    var request = new XMLHttpRequest();

    this._configureCallbacks(request);

    if (emulateHTTP) {
      this.body = this.body || {};
      if (typeof this.body === 'object') this.body._method = method;
      requestMethod = 'POST';
    }

    request.open(requestMethod, this.url, true);
    if (emulateHTTP) request.setRequestHeader('X-HTTP-Method-Override', method);

    this._setContentTypeHeader(request); // HERE THE CONTENT-TYPE IS SET
    this._setUserDefinedHeaders(request); // HERE THE CONTENT-TYPE IS SET AGAIN (DUPLICATION)

    var args = [];
    if (this.body !== undefined) {
      args.push(Utils.params(this.body));
    }

    request.send.apply(request, args);
  },

Thanks :)

@tulios
Copy link
Owner

tulios commented Oct 12, 2016

Hi @ericholiveira, thanks for the report. Can you try to set your header to content-type, all in lowercase?

Maybe this (https://github.com/tulios/mappersmith/blob/master/src/gateway/vanilla-gateway.js#L124) is failing, this is definitely a bug, I'll take a look.

Thanks again.

@tulios tulios added the Bug label Oct 12, 2016
@ericholiveira
Copy link
Author

Ive tried with 'content-type' but still doenst work... actually i think the line of code you highlighted works... the problem is in _setContentTypeHeader you call request.setRequestHeader('Content-Type', contentType); which sets the correct content type... but on the next function call _setUserDefinedHeaders you are just setting all headers again (including content-type) and for this reason it duplicate this header. A simple filter on _setUserDefinedHeaders to avoid setting content-type again should do the trick.

@tulios tulios closed this as completed in 28b6b83 Oct 30, 2016
@tulios
Copy link
Owner

tulios commented Oct 30, 2016

@ericholiveira This is a quick fix but it should work well. I'm planning a major refactor on mappersmith internals. Thanks again!

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