Status code: 200 when trying to create project from API

Must-share information (formatted with Markdown):

  • which versions are you using
    Enterprise Edition Version 10.2.1 (build 78527)

  • what are you trying to achieve
    Create a project using api

  • what have you tried so far to achieve this

I am using the below to search the existing projects & it works fine,

def search_project(api_key):
    headers = {
        "Authorization": f"Bearer {api_key}"
    }

    params = {
        "q": var.sonar_project_key
    }

    response = requests.get(api_url, headers=headers, params=params)
    return response.json()

but when using the below to create a new project, it is giving a status code 200 & the project is not getting created.

def create_project(api_key, working_branch=None):
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    payload = {
        "name": var.sonar_project_key,
        "project": var.sonar_project_key,
        "visibility": "private"
    }

    if working_branch:
        payload["mainBranch"] = working_branch

    response = requests.post(api_url, json=payload, headers=headers)
    return response

I tried using it with / without the branch, visibility etc but still getting the 200 response. I also tried using a user token as well as a global analysis token but there is no change in the response.

Hi,

Welcome to the community!

Have you tried this via the UI? The best way to master the API is to perform the desired action via the UI and eavesdrop to see which calls the UI made to accomplish the action.

You may also find this guide helpful.

 
Ann