0

I have a .Net API exposed, running on an IIS Server. There is a Windows scheduled task that executes nightly. It runs a cURL request. When I copy that command and execute it in a cmd window, it returns a 401.

I try the same api call in Postman, and get a 200.

enter image description here

IIS logs show:

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

My cURL command is this:

-X POST https://example.Com/api/tasks/checkstaledirectdebits -H "Connection: keep-alive" -H "Content-Type: application/json" -H "X-API-Key: MyValidKey" -H "Accept: ." -H "Host: api.kikka.com.au" -H "cache-control: no-cache" -H "Content-Length: 0"

And IIS logs:

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

Both the cUrl command, and the Postman instance are running on the server. The same one the API is running on. Am I missing something that Postman does, that I need to make cURL do?

Craig
  • 18,074
  • 38
  • 147
  • 248

1 Answers1

1

Firstly, I suggest you generate an equivalent HTTP request by using PostMan Code Snippet Genertator. This could guarantee that these requests are absolutely in the same. enter image description here
enter image description here
For more details check the below links.
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/
Secondly, I doubt that there are some redirection operations occurred during the request resulting in the HTTP header(X-API-Key) lost, so that the authentication failed. Please carefully check the URL.
Here is a careless demonstration.
HttpClient returns 401 with correct authorisation header
Feel free to let me know if there is anything I can help with.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22