【问题标题】:How to send get request to a web API using CURL?如何使用 CURL 向 Web API 发送获取请求?
【发布时间】:2021-01-14 18:38:41
【问题描述】:

我有一个 Web API,当我使用 Postman 向它发送 get 请求时它工作正常。例如,API 可以处理这个请求:

http://localhost:5000/api/test/?id=23

当我用 curl ($ curl http://localhost:5000/api/test/?id=23) 尝试同样的事情时,我得到了这个错误:

curl: (3) URL using bad/illegal format or missing URL

这里有什么问题?

【问题讨论】:

  • 尝试通过 Postman 的代码功能导出 curl。也许您在 Postman 中的请求中有标头,而您没有在 curl 中发送这些标头。
  • @ChristianBaumann 谢谢,我该怎么做?我在 Postman 中看到 Import,但没有看到 Export。
  • 我刚刚运行了你的 curl 语句 (curl http://localhost:5000/api/test/?id=23) 并得到了 curl: (7) Failed to connect to localhost port 5000: Connection refused,因为我没有在端口上运行任何东西......语句应该没问题

标签: api curl postman webapi


【解决方案1】:

您的应用可能只支持 POST 请求? curl http://localhost:5000/api/test/?id=23 将发送 GET 请求。

试试

curl -X POST http://localhost:5000/api/test/?id=23

如果这不起作用,原因可能在于请求标头、cookie 或(不太可能)API 中的用户代理处理。

【讨论】:

  • ?id=23 表示GET 那么为什么要将它作为POST 发送呢??
  • 这当然不是一个好习惯,但这是可能的,并且是我首先要检查的。
  • 有效点。我认为如果没有上下文,在不了解 HTTP 请求方法的情况下偶然发现此问题的人可能无济于事。所以也许可以补充一点解释?
猜你喜欢
  • 2018-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多