Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl will not return while performing a HEAD request

Tags:

node.js

curl

etag

I am creating resources on a node server and attaching an ETag to each one of them. I am using the method found in this question What encoding should I use to properly generate an ETag with crypto in nodeJS? for generating the ETags. However, when I do a HEAD request on command line using curl, like:

$ curl -i -u username:password -X HEAD http://localhost:3000/aResource/0

the answer is

HTTP/1.1 200 OK
X-Powered-By: Express
ETag: "1a37c148692f92ad6c97887f77c873e6"
Content-Type: text/plain
Date: Sat, 26 Oct 2013 01:06:28 GMT
Connection: keep-alive

^C
$

and I have to specifically press Control-C in order to see the command prompt again. On the other hand, I find this strange, because the way I am handling the HEAD request is through app.head on node and after the authentication I am calling the function:

function serveResourceEtag(req, res) {
    console.log("Following route through HEAD");
    var anID    = parseInt(req.params[resourceID], "utf8");
    if (anID < serverData.resourceEtags.length) {
        res.writeHead (200, {"ETag": "\"" + serverData.resourceEtags[anID] + "\"", "Content-Type": "text/plain"});
        res.end();
    }
    else {
        res.writeHead (404, {"Content-Type": "text/plain"});
        res.end();
    }
};

In other words, even though I have the res.end(); command in the end, the transmission for some reason has not stopped. As far as I understand a HEAD request should get the same header as a GET request. The difference with a GET is that in the case of HEAD we do not have the body of a GET request. Can you tell what seems to be the problem here?

like image 809
MightyMouse Avatar asked Oct 26 '13 01:10

MightyMouse


1 Answers

You're right about the behaviour of the HTTP methods, it's just the curl that's wrong, you want:

curl -I -u username:password http://localhost:3000/aResource/0

not -X HEAD which is saying use a HEAD method of HTTP rather than a GET which only returns the headers

like image 85
Matt Newman Avatar answered Sep 29 '22 12:09

Matt Newman