我正在尝试使用 Linux 上的命令行从本地主机服务器获取页面。
我的 Node 的 REST 端点如下所示:
app.get('/autogen',function(req, res) {
res.set('Content-Type', 'text/plain');
res.status(200).send("OK");
});
在我的浏览器上访问这里效果很好:
但是,我不知道如何使用 CURL 通过命令行执行相同的 GET 请求。
这是我尝试过的:
curl -H "接受:*/*"-X GET http://localhost:8084/autogen -v
curl --ipv4 -H "接受:*/*"-X GET http://localhost:8084/autogen -v
curl -i -H "接受:*/*"-X GET http://localhost:8084/autogen -v
curl -H“接受:text/html”-H“内容类型:text/html”-X GET http://localhost:8084/autogen -v
curl -H "接受:text/html"-X GET http://localhost:8084/autogen -v
curl -H“接受:文本/纯文本”-H“内容类型:文本/纯文本”-X GET http://localhost:8084/autogen -v
curl -I -H "接受:*/*"-X GET http://localhost:8084/autogen -v
curl --ipv4 -X GET http://localhost:8084/autogen -v
所有 CURL 命令返回响应:HTTP/1.1 204 无内容
====================================
日志和信息
以下是上面来自 CURL 命令的响应消息的完整日志(用 {} 替换部分):
* About to connect() to proxy {MYCOMPANYPROXY} port 8080 (#0)
* Trying {MYIP}... connected
* Proxy auth using Basic with user '{MYNAME}'
> GET http://localhost:8084/autogen HTTP/1.1
> Proxy-Authorization: Basic {...}
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:8084
> Proxy-Connection: Keep-Alive
> Accept: text/plain
> Content-Type: text/plain
>
< HTTP/1.1 204 No Content
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Type: text/html; charset=utf-8
< Proxy-Connection: Keep-Alive
< Connection: Keep-Alive
< Content-Length: 21958
<
* Excess found in a non pipelined read: excess = 3013 url = /autogen (zero-length body)
* Connection #0 to host {MYCOMPANYPROXY} left intact
* Closing connection #0
这里还有通过 chrome 查看页面的完整标题:
此外,我的 no_proxy 环境变量设置为不在 localhost 上使用:
echo $no_proxy
localhost,127.0.0.1,*.dom1,*.grp
这是我正在使用的版本:
**Express Version: 3.14.0
**Node Version: v6.5.0
**Curl version: curl 7.22.0 (x86_64-pc-linux-gnu)
另外,旁注,当有人访问该页面时,我的服务器会告诉我,例如
GET /autogen 200 10.168 ms - 2
GET /autogen 304 1.617 ms - -
但是当我使用curl命令时我什么也没得到,所以它一定没有被解析或者什么东西
最佳答案
@dave_thompson_085 帮我解决了这个问题!
显然 chrome 没有使用代理,而curl是,而且我不想使用代理。
原因是我的curl设置文件~/.curlrc
。它包含这样的内容:
proxy=http://myuser:mypass@myproxy
所以,我删除了 ~/.curlrc
和 BAM!现在可以工作了!
谢谢!
关于node.js - 如何使用curl 在我的本地主机 Node 服务器上执行GET?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44145984/