S
S
slden2020-10-12 11:17:07
linux
slden, 2020-10-12 11:17:07

Why does curl -I (--head) return a 404 code, but curl -i returns a 200 code?

Actually, in the curl command, both -I and -i switches must return a response code. But in my case, they (response codes) are different for some reason.
Through the browser everything works fine.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Melkij, 2020-10-12
@melkij

curl 127.0.0.1 -I

[email protected]:~$ nc -l 8081
HEAD / HTTP/1.1
Host: 127.0.0.1:8081
User-Agent: curl/7.64.0
Accept: */*

curl 127.0.0.1:8081 -i
[email protected]:~$ nc -l 8081
GET / HTTP/1.1
Host: 127.0.0.1:8081
User-Agent: curl/7.64.0
Accept: */*

And what prevents the web server from responding differently to different requests?

D
Dmitry, 2020-10-12
@q2digger

and if you add User-Agent ? something like

curl -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" blah blah

V
Victor Taran, 2020-10-12
@shambler81

5f841bb2f143b334402655.png

curl -I http://ya.ru 2>/dev/null | head -n 1 | cut -d$' ' -f2
curl -i http://ya.ru 2>/dev/null | head -n 1 | cut -d$' ' -f2

Can you show everything that you have written?

S
shurshur, 2020-10-12
@shurshur

Depending on the libraries and technologies used, the HEAD handler may also need to be explicitly written. Therefore, it is not surprising that the server is not required to return the same.
For example, here I have a script written a hundred years ago based on the BaseHTTPServer library:

def do_GET(self):
  url = self.path
  for mm in self.methodmap.keys():
    if re.match(mm, url):
      return self.methodmap[mm](url, None)
  self.send_response(404)
  self.send_header('Content-Type', 'text/plain')
  self.end_headers()
  self.wfile.write('URL not defined')

def do_HEAD(self):
  self.send_response(200)
  self.send_header('Content-type', 'application/json')
  self.end_headers()

Here HEAD will always return 200, even for crooked URLs, while GET can return 404. Because there was no need for me to write beautifully for HEAD, and I just left the option that was in the default script template.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question