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

时间:2020-06-04 03:26:48

标签: api curl iis iis-8

我公开了一个运行在IIS服务器上的.Net API。 Windows计划的任务每晚执行一次。它运行一个cURL请求。当我复制该命令并在cmd窗口中执行该命令时,它将返回401。

我尝试在Postman中进行相同的api调用,并获得200。

enter image description here

IIS日志显示:

2020-06-04 02:59:55 W3SVC3 127.0.0.1 POST / api / tasks / checkstaledirectdebits-443-127.0.0.1 PostmanRuntime / 7.25.0--example.com 200 0 0 557

我的cURL命令是这样的:

-X POST https://example.Com/api/tasks/checkstaledirectdebits -H“连接:保持活动状态” -H“内容类型:application / json” -H “ X-API-Key:MyValidKey” -H“接受:” -H “主机:api.kikka.com.au” -H“缓存控制:无缓存” -H “内容长度:0”

和IIS日志:

2020-06-04 02:58:37 W3SVC3 127.0.0.1 POST / api / tasks / checkstaledirectdebits-443-127.0.0.1 curl / 7.59.0-- example.com 401 0 0 4640

cUrl命令和Postman实例都在服务器上运行。 API正在运行相同的对象。我是否错过了邮递员要做的事情,我需要让cURL做?

1 个答案:

答案 0 :(得分:0)

首先,我建议您使用PostMan Code Snippet Genertator生成等效的HTTP请求。这样可以保证这些请求完全相同。 enter image description here
enter image description here
有关更多详细信息,请检查以下链接。
Converting a POSTMAN request to Curl
https://learning.postman.com/docs/postman/sending-api-requests/generate-code-snippets/
https://blog.postman.com/curl-and-postman-work-wonderfully-together/
其次,我怀疑在请求过程中是否发生了一些重定向操作,导致HTTP标头(X-API-Key)丢失,从而导致身份验证失败。请仔细检查网址。
这是一个粗心的示范。
HttpClient returns 401 with correct authorisation header
随时让我知道是否有什么可以帮助您的。