Recently I get this error while running a python script to test POST API Request.
Error: HTTPError: 401 Client Error: Unauthorized for URL:
The weird thing here is that It is working well with the same mechanism for GET API Requests.
I double-checked my credentials to make sure they are not the cause. I can access the same POST API via SWAGGER & Postman and executing them successfully.
I also debugged the following script, and I see that It fails when It reaches the “POST On Session” code line.
Create Session test ${base_url} verify=true
${RequestBody}= create dictionary testSku="string" testCode="string"
${RequestHeader}= create dictionary Content-Type=application/json
${resp}= POST On Session test /catalog/product/lookup/ data=${RequestBody} headers=${RequestHeader}
Should Be Equal As Strings ${resp.status_code} 200
Since you have working examples with SWAGGER & Postman, I suggest you grab the request body and headers from your successful example and compare that to the request body and headers generated by RequestsLibrary, when you find a difference you’ll be in the track to figuring out the solution. It could be a missing/incorrect/extra header, or it could be something in the request body.
Are you able to post them here? I could have a look as sometimes it’s not obvious.
I need a working and a not working example to see the difference.
Also is there any response body, 401 is usually a not authorised, but I didn’t see any username password being entered anywhere, it may not need to be on this specific post request as long as it’s in the session, but it seems you are creating the session and then immediately doing a post request, do you need to do a login request first? or do you need to pass the authorisation credentials in the headers? or some other way?
Check with you SWAGGER & Postman examples and see how they are doing the authentication.
and then called it inside the test case to be run firstly
*** Test Cases ***
TC0002_POST_test
${cookies}= State Machine Authentication Service API
create session testsession ${base_url} verify=true proxies=https cookies=${cookies}
I am not sure if I am using the “proxies=https” successfully or not, but I thought it may be the reason, so I inserted it.
I would leave the proxies= bit out unless you know you need to go through a proxy server
FYI a proxy server address should look something like this: https://proxyserver:port/
but the documentation says that the proxies option requires a dictionary object, containing the proxy servers, not sure how that need to be formatted, but what you put there will defiantly not work.