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

m.request sends content-type when there is no content. #280

Closed
lawrence-dol opened this issue Sep 24, 2014 · 1 comment
Closed

m.request sends content-type when there is no content. #280

lawrence-dol opened this issue Sep 24, 2014 · 1 comment
Labels
Type: Bug For bugs and any other unexpected breakage

Comments

@lawrence-dol
Copy link

Currently, in the bowels of m.render we see:

function ajax(options) {
    ...
    if (options.serialize == JSON.stringify && options.method != "GET") {
        xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    }
    ...
    xhr.send(options.method == "GET" ? "" : options.data)
    return xhr
}

However, this is incorrect; it should test options.data && options.method!="GET" in both cases, since in the HTML protocol if the Content-Type is specified and Content-Length is not then the server should read until the end of the request, and omitting Content-Length is highly undesirable and permitted primarily for backward compatibility with HTTP 1.0. Some servers, and the one I am using is one of them, will reject an API request if the content length is not given but the content type is since it's defeats HTTP 1.1 keep-alive.

Putting it more succinctly, if there is no content, don't set the content-type header.

Where I am seeing this fail is on RESTful DELETE requests, which quite naturally have no request content. I've worked around it by detecting an omitted data object and setting it to "" in my wrapper, which then causes Content-Length: 0, but the right solution is to fix Mithril (IMO).

(As an aside, the HTTP spec technically permits content with a GET request, though with severe drawbacks relating to proxies and caching, so while it's unusual the data should not, technically, be omitted for GET. However, the current implementation may be convenient, so having an option to send content with a GET may be desirable, though it's not what I am asking for here.)

@lhorie lhorie added the Type: Bug For bugs and any other unexpected breakage label Sep 25, 2014
@lhorie
Copy link
Member

lhorie commented Sep 25, 2014

Thanks. I've added a patch in the code in origin/next. This will be released in 0.1.22

@lhorie lhorie closed this as completed Sep 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug For bugs and any other unexpected breakage
Projects
None yet
Development

No branches or pull requests

2 participants