JerrySI
JerrySI

Reputation: 11

Random 401 Unauthorized when calling REST API from bash

I have a strange issue with REST calls. On the server side I have Windwos machine with IIS

I have a list of IDs (approximately 8000 - I tried also with less records) for each of this IDs I need to do 3 different REST calls (2x GET and one PUT).

My authentication is on the beginning and I get session cookie, which is then stored for all further calls to do authorization. At the end I do Logout.

Now the problem. Using the C# on Windows the above logic works as expected. On Linux bash, from time to time, completely randomly I get the 401 Unauthorized error. So it is not related to any specific ID nor REST call. The next call with same cookie is again successful. Do you have any idea what could cause the issue. The customer, uses JAVA on LINUX and they have the same issue.

I tried with sleep time for a second between call, but it didn't work.

I enabled full trace on IIS and verbose on the bash call and on the IIS I don't see those 401 requests at all, while verbose clearly indicates 401. If I don't do the authentication on the beginning all unsuccessful attempts are clearly visible in the IIS log.

#!/bin/bash

#--------------------- LOGIN CALL ---------------------
"" > output.txt

curl -s -c cookie.txt -X POST "https://www.myapi.si/auth/apphost" -H "Content-Type: application/json" -d '{"authString":"Module=DialogUser;User=userTest;Password=Password123."}'

#--------------------- CALL GET ---------------------

objectId=("104569", "105079", "103103", "97267", "106385") 
for item in "${objectId[@]}"; do
    #--------------------- CALL GET CCCEmployeeDataSlim ---------------------
    echo "$item"
    echo "$item" >> output.txt
    myDate=$(date -u +"%H:%M:%S")
    echo "$myDate" >> output.txt
    curl -s -b "cookie.txt" -X GET --no-buffer "https://www.myapi.si/api/entities/GetObjectData?where=objectId='$item'&loadType=BulkReadOnly" -H "Accept: application/json" >> output.txt
    sleep 1
    echo "" >> output.txt
    echo "....................." >> output.txt
    #--------------------- CALL GET CCC_IsAccountValid ---------------------
    curl -s -b "cookie.txt" -X PUT -H "Content-Type: application/json" --no-buffer "https://www.myapi.si/api/script/IsAccountValid" -d '{"parameters": ["'$item'","2024-9-19"]}' >> output.txt
    sleep 1
    echo "" >> output.txt
    echo "....................." >> output.txt
    #--------------------- CALL GET CCCSecondaryIdentities ---------------------
    curl -s -b "cookie.txt" -X GET --no-buffer "https://www.myapi.si/api/entities/GETSecondaryIdentities?where=objectId='$item'&loadType=BulkReadOnly" -H "Accept: application/json" >> output.txt
    sleep 1
    echo "" >> output.txt
    echo "--------------------- NEXT ---------------------" >> output.txt
done

Upvotes: 0

Views: 80

Answers (1)

RMacroman
RMacroman

Reputation: 11

This 401 is probably coming from the firewall, which is why it doesn't even reach the IIS verbose, there may be a rate limit!

Upvotes: 0

Related Questions