ConnectionError when calling a POST Request for the 2nd time. 1st call passes

Hi, I’m stuck with this issue since a month now & I have no idea what other solutions are possible.

So we have a microservice springboot API hosted on my localhost & I have automated the testing using Python scripts. Request, response is json.

When I make a POST request with json body, first request goes through & passes
Subsequent calls to POST, fail with Connection Error in adapter.py - requests.exceptions.ConnectionError: (‘Connection aborted.’, FileNotFoundError(2, ‘No such file or directory’))
I have am not reading any file outside the test so I don’t get why FileNotFound.

When I debug, I see no response from httplib_response = self._make_request(…) in connectionpool.py & ends up going to EmptyPoolError.

Any number of consecutive GET, PATCH methods pass without this issue. Only Post is failing.

Manual test works fine any number of times on Postman.

Please suggest how to resolve this blocker.

My code -
def test_check_decrypt_API():
sample_ciphertext = “ahufjsdjkf”

# Open a new session
s = requests.session()
s.headers.update(header)

# Post the API request
response = s.post(decryptUrl, json={"Encoded_Data": sample_ciphertext, }, headers=header, verify=False)

# Verify response
print(response.json())
assert response.status_code == 200
response.close()

It seems like the issue could be related to the way the POST request is being handled by your Python test script, especially since it works fine when you test it manually using Postman.

First, try adding a small delay between the requests in your script to ensure there’s enough time for connections to be properly closed and to avoid running out of available connections in the pool.

Here’s an updated version of your code with the time.sleep(1) added:

import time

def test_check_decrypt_API():
    sample_ciphertext = "ahufjsdjkf"

    # Open a new session
    s = requests.session()
    s.headers.update(header)

    # Add a small delay between requests
    time.sleep(1)

    # Post the API request
    response = s.post(decryptUrl, json={"Encoded_Data": sample_ciphertext, }, headers=header, verify=False)

    # Verify response
    print(response.json())
    assert response.status_code == 200
    response.close()

If that doesn’t help, consider updating the Connection: keep-alive header to Connection: close in your header dictionary to explicitly close the connection after each request:

header = {
    # other headers
    'Connection': 'close',
}

Additionally, it’s a good practice to use the with statement when using the requests.Session object. This ensures that the session is properly closed after the block of code is executed:

import time

def test_check_decrypt_API():
    sample_ciphertext = "ahufjsdjkf"

    header = {
        # other headers
        'Connection': 'close',
    }

    # Open a new session
    with requests.session() as s:
        s.headers.update(header)

        # Add a small delay between requests
        time.sleep(1)

        # Post the API request
        response = s.post(decryptUrl, json={"Encoded_Data": sample_ciphertext, }, headers=header, verify=False)

        # Verify response
        print(response.json())
        assert response.status_code == 200
        response.close()

If you still face the issue, try investigating the server logs for any errors or issues related to handling POST requests. This might help in identifying any specific issues with the server configuration.

What is header here? I have a vague suspicion that malformed headers could cause the issue you’re seeing.