Marialena
Marialena

Reputation: 817

Python script which access GitLab works on Windows but returns 'Project Not Found' on Windows Subsystem for Linux (WSL) - Used python requests

I have a python script which does a GET request to GitLab and stores the data from the response in an excel file using tablib library.

This script works fine in Windows when I execute it using python3.

I have tried to execute the same script in the Windows Subsystem for Linux (WSL) I have enabled and the script fails.

The output when I execute with python3 script.py in WSL is the following:

RESPONSE {"message":"404 Project Not Found"}

When I execute from Windows using python .\gitlab.py where python is python3:

RESPONSE [{"id":567,"iid":22}, {"id":10,"iid":3}]

I think the problem could be related to the GET api call I am doing because in WSL it returns Project Not Found.

I executed that request using curl in WSL to see if the unix in general has this issue, but I get back the expected response instead of the not found response. This was the request:

curl -X GET   'https://URL/api/v4/projects/server%2Fproducts%2FPROJECT/issues?per_page=100'   -H 'Content-Type: application/json' -H 'PRIVATE-TOKEN: TOKEN' --insecure

Why is python failing in unix using Python if unix is able to execute the get request using curl? Should I enable/disable something in the request perhaps?

This is the request I am doing in my python script:

def get_items():

    url = "https://URL/api/v4/projects/server%2Fproducts%2FPROJECT/issues"

    payload = {}

    querystring = {"state": "closed", "per_page": "100"}

    headers = {
        'Content-Type': "application/json",
        'PRIVATE-TOKEN': os.environ.get("GITLAB_KEY") # enviromental variable added in windows 
    }

    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

    response = requests.request(
        "GET", url, headers=headers, data=payload, params=querystring,  verify=False)

    print("RESPONSE " + response.text)
    return json.loads(response.text)

UPDATE:

I have tried using the project id as well instead of the path but it didn't work

Upvotes: 1

Views: 460

Answers (2)

Marialena
Marialena

Reputation: 817

Based on an answer I got in the post I did in Reddit, I found the problem.

In the python script, I am using an environmental variable which is not accessible in that way ( os.environ.get("GITLAB_KEY") ) from the WSL.

For now, I have replaced it with the hard-coded value just to check that this was really the issue. The script now works as expected.

I will find a way to access the env var again now that I know what the problem was.

Upvotes: 0

awm
awm

Reputation: 2768

REF: https://docs.gitlab.com/ee/api/projects.html#get-single-project

GET /projects/:id

Change this: url = "https://URL/api/v4/projects/server%2Fproducts%2FPROJECT/issues" To projectId = 1234 # or whatever your project id is ... Project Page, Settings -> General url = "https://URL/api/v4/projects/" + projectId + "/issues"

Upvotes: 1

Related Questions