How can I filter the result of the api call "Get projects paginated" using the QUERY PARAMETERS in python?

Hi,
I’m trying to get the key information of the project using the project name for one automation that i’m working on in python. It is the first time i’m doing that, so i’m strugling with placing the query parameter “query” correctly.

The definition of “query” parameter in jira api is:
Filter the results using a literal string. Projects with a matching key or name are returned (case insensitive).

It is exactly what I need. I can get the list of projects, but I can’t make the filter work. Can someone tell me how is the correct way to place the query parameter on the code below?

# This code sample uses the 'requests' library:
import requests
from requests.auth import HTTPBasicAuth
import json

url = "/rest/api/3/project/search"
auth = HTTPBasicAuth("email@example.com", "<api_token>")
headers = {
   "Accept": "application/json"
}
query = {
        "query" : 'sample-project',  # I'm trying that way, not working.
    }

response = requests.request(
   "GET",
   url,
   headers=headers,
   auth=auth
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

@paulosilva,

The query is a URL parameter so it belongs in your url variable, along with a full HTTP address:

sitename = ""
url = f"https://{sitename}.atlassian.net/rest/api/3/project/search?query=sample-project"

I’ve been bitten by string manipulation of URLs too many times so, regardless of programming language, I reach for a URI templates library for safer construction of URLs from variables. In Python, I use uritemplate.

from uritemplate import URITemplate

getProjectPaginated = URITemplate("{+base_url}/rest/api/3/project/search{?query}")
myBaseUrl = ""
myQuery = ""
url = getProjectPaginated.expand({
    "base_url": myBaseUrl,
    "query": myQuery})

Otherwise, your use of requests looks fine.

1 Like

Thank you @ibuchanan! It really works!!

I’m still learning and discovering how to use API and python to build an automation to manage the users. Because I’m doing that manually and it is very time consuming.

I hope with that automation I’ll have time to keep studying and automate other things.

1 Like