邮递员在调用API时返回200,但cURL命令返回401。

问题描述 投票:0回答:1

我有一个.Net API暴露,运行在IIS服务器上。有一个Windows计划任务,每晚都会执行。它运行一个cURL请求。当我复制该命令并在cmd窗口中执行时,它返回一个401。

我在Postman中尝试同样的api调用,得到的是200。

enter image description here

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来做?

api curl iis iis-8
1个回答
0
投票

首先,我建议你生成一个等价的HTTP请求,通过使用 PostMan Code Snippet Genertator. 这可以保证这些要求绝对是一致的。enter image description here enter image description here 更多细节请查看下面的链接。将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,授权头是正确的 如果有什么需要我帮忙的,请随时告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.