【问题标题】:Getting curl: (6) Could not resolve host: localhost获取卷曲:(6)无法解析主机:localhost
【发布时间】:2019-06-14 04:49:46
【问题描述】:

获取 curl:(6) 无法解析主机:localhost

我在通过 curl 调用本地主机 URL 时遇到问题。我确实检查了this question,但它对我没有帮助。

我的 curl 命令:

curl --data "ip=127.0.0.1&device_type=web" http://localhost/api/users/getToken.json

还尝试了这个选定答案中的解决方案:https://stackoverflow.com/a/40078901/1225070

curl -H "Content-Type: application/json" --data "ip=127.0.0.1&device_type=web" http://localhost/api/users/getToken.json

但是,如果我使用 IP 地址运行上述命令,则它可以工作

curl --data "ip=127.0.0.1&device_type=web" http://192.168.1.1/api/users/getToken.json

任何帮助为什么在使用 localhost 时这不起作用?

【问题讨论】:

  • 如果 IP 适合你,你为什么不使用它?
  • @Moin,我可以使用 IP 但我想知道为什么 localhost 不起作用。

标签: curl


【解决方案1】:

我遇到了同样的错误,发现我的 /etc/hosts 没有它应该有的本地主机映射。

如果您运行 cat /etc/hosts 并且它没有显示包含以下内容的行,那么您遇到了与我相同的问题:

127.0.0.1 localhost

要解决此问题,只需在您的 /etc/hosts 文件中添加与上一行类似的行。你也可以通过运行:

sudo echo "127.0.0.1 localhost" >> /etc/hosts

【讨论】:

    猜你喜欢
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多