bash, 52 bytes
curl -IL codegolf.stackexchange.com/q/$1|grep 2\ 200
Takes input as a command line argument, and outputs in the form of exit code: 0
or "success" (truthy) for an undeleted question, and 1
or "failure" (falsy) for a nonexistent or deleted question.
It also prints a bunch of junk to STDERR and potentially STDOUT, but this is allowed to be ignored as per meta.
As an explanation, the -I
flag to curl
sends an HTTP HEAD request (fetch only the headers), and -L
will follow redirects (http to https, and then /q to /questions). The full output from the curl
command will look something like this:
HTTP/1.1 301 Moved Permanently
Location: https://codegolf.stackexchange.com/q/127030
X-Request-Guid: 1a0b6059-057c-448c-bfc9-a96f1b373abf
Content-Length: 160
Accept-Ranges: bytes
Date: Tue, 20 Jun 2017 04:07:30 GMT
Via: 1.1 varnish
Age: 503
Connection: keep-alive
X-Served-By: cache-itm7423-ITM
X-Cache: HIT
X-Cache-Hits: 1
X-Timer: S1497931651.709631,VS0,VE0
Vary: Fastly-SSL
X-DNS-Prefetch-Control: off
Set-Cookie: prov=933ae4b0-81b0-2b37-e26e-59a0fbb6991a; domain=.stackexchange.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
HTTP/2 302
date: Tue, 20 Jun 2017 04:07:31 GMT
content-type: text/html; charset=utf-8
location: https://codegolf.stackexchange.com/questions/127030/helping-danny-to-maximize-his-score
x-frame-options: SAMEORIGIN
x-request-guid: fdcaf556-7ae6-4240-89a1-ace7625f8821
strict-transport-security: max-age=15552000
accept-ranges: bytes
via: 1.1 varnish
age: 0
x-served-by: cache-nrt6132-NRT
x-cache: MISS
x-cache-hits: 0
x-timer: S1497931651.085546,VS0,VE166
vary: Fastly-SSL
x-dns-prefetch-control: off
set-cookie: prov=5e314a4f-0c50-08ef-afa2-9388b37e3b7a; domain=.stackexchange.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
cache-control: private
content-length: 204
HTTP/2 404
date: Tue, 20 Jun 2017 04:07:31 GMT
content-type: text/html; charset=utf-8
x-frame-options: SAMEORIGIN
x-request-guid: 3f72afb7-81e8-4e69-a8ad-474534f2abe6
strict-transport-security: max-age=15552000
accept-ranges: bytes
via: 1.1 varnish
age: 0
x-served-by: cache-nrt6132-NRT
x-cache: MISS
x-cache-hits: 0
x-timer: S1497931651.323339,VS0,VE189
vary: Fastly-SSL
x-dns-prefetch-control: off
set-cookie: prov=cede4119-eba5-d799-e376-848e4e457f07; domain=.stackexchange.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
cache-control: private
content-length: 42158
Note that the 2\ 200
(equivalent to '2 200'
) will match the line HTTP/2 200
if it exists (HTTP OK), and it will not match HTTP/2 404
(Not Found) for questions that do not exist. (The inclusion of the 2
is necessary to avoid false positives if some of the other headers happen to contain the sequence 200
.)
The question must not have been removed, but may be open or closed.
This is to benefit answers that don't use the Stack Exchange API because, I believe, a page will give 404 Not Found iff it is not a question or was removed \$\endgroup\$