GJain
GJain

Reputation: 5093

Node.js: curl: (52) Empty reply from server with space in request not encoded

var http = require('http');

var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

server.listen(8000);
console.log("Server running at http://127.0.0.1:8000/");

I executed following curl commands:

curl "http://127.0.0.1:8000/"
Hello World

// space is not encoded
curl "http://127.0.0.1:8000/x y"
curl: (52) Empty reply from server

curl "http://127.0.0.1:8000/x"
Hello World

// space is encoded
curl "http://127.0.0.1:8000/x%20y"
Hello World

Can you please explain the why I get curl 52???

In this case, I want to send 500 back. Can I do that?

Upvotes: 7

Views: 10894

Answers (2)

Brad Pitt
Brad Pitt

Reputation: 416

I'm having this question in mind as well. I guess curl expects already-encoded url if quoted by double quotes. If it finds whitespace in url, it will consider it an invalid url.

And this is quite different from wget command. If you run this:

wget "http://127.0.0.1:8000/x y"

actually wget encodes the url for you and the request actually will be sent as:

http://127.0.0.1:8000/x%20y

These kind of facts really tease our brains.

Upvotes: 0

Morgan ARR Allen
Morgan ARR Allen

Reputation: 10678

Even with the missing res.send it looks like an issue with your route. you probably meant.

app.get('/item/:id', function(...) {
  ..
})

Note the : before id. This creates a variable that can be accessed on req.params.id.

Upvotes: 1

Related Questions