我有一个.Net API暴露,运行在IIS服务器上。有一个Windows计划任务,每晚都会执行。它运行一个cURL请求。当我复制该命令并在cmd窗口中执行时,它返回一个401。
我在Postman中尝试同样的api调用,得到的是200。
IIS日志显示。
2020-06-04 02:59:55 W3SVC3 127.0.0.1 POST apitaskscheckstaledirectdebits - 443 - 127.0.0.1 PostmanRuntime7.25.0 - - example.com 200 0 0 557
我的cURL命令是这样的。
我的cURL命令是这样的: -X POST https:/example.Comapitaskscheckstaledirectdebits。 -H "Connection: keep-alive" -H "Content-Type: applicationjson" -H "X-API-Key: MyValidKey" -H "Accept: ."-H "Host: api.kikka.com.au" -H "cache-control: no-cache" -H "Content-Length: 0"
还有IIS日志。
2020-06-04 02:58:37 W3SVC3 127.0.0.1 POST apitaskscheckstaledirectdebits - 443 - 127.0.0.1 curl7.59.0 - - example.com 401 0 0 4640
cUrl命令和Postman实例都在服务器上运行。就是API运行的那台服务器。我是否遗漏了一些Postman的功能,而我需要让cURL来做?
首先,我建议你生成一个等价的HTTP请求,通过使用 PostMan Code Snippet Genertator
. 这可以保证这些要求绝对是一致的。 更多细节请查看下面的链接。将POSTMAN请求转换为Curl请求 https:/learning.postman.comdocspostmansending-api-requestsgenerate-code-snippets。 https:/blog.postman.comcurl-and-postman-work-wonderfully-together。 其次,我怀疑是在请求过程中发生了一些重定向操作,导致HTTP头(X-API-Key)丢失,从而导致认证失败。请仔细检查URL。下面是一个不小心的示范。HttpClient返回401,授权头是正确的 如果有什么需要我帮忙的,请随时告诉我。