Troubleshooting 400 with Bulk Component creation via API

Gabriel Roth February 19, 2025

I am using the Rest v3 API to bulk create a component that needs to exist in several (a few dozen) projects. I am trying to automate this using Python by looping over a .csv. I am getting a 400 error, but I cannot figure out how to get this to work. Any help would be greatly appreciated.

My .csv looks like this:

Screenshot 2025-02-19 at 11.18.38 AM.png

My Python script looks like this:

 

import requests
from requests.auth import HTTPBasicAuth
import json
import csv
from dotenv import load_dotenv
import os

# Jira Cloud URL - update this value to your cloud url

# API endpoint for adding users to a group
api_endpoint = f"{jira_url}/rest/api/3/component"

# Authentication credentials - update the email and api_token for your user and token combination
# these should be added to a .env file and also excuded from .gitignore
email = os.getenv("email")
api_token = os.getenv("api_token")

# Read the CSV file with user and group IDs
csv_file_path = "Admin Automation/bulk_component_add.csv"

auth = HTTPBasicAuth(email, api_token)

headers = {
    "Accept": "application/json",
    "Content-Type": "application/json",
}

# Process the CSV file
with open(csv_file_path, mode="r") as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        project = row["project"] # Change to match your CSV column names
        name = row["name"] # Change to match your CSV column names
        assigneeType = row["assigneeType"] # Change to match your CSV column names
        projectId = row["projectId"] # Change to match your CSV column names


        # Construct the JSON payload
        payload = json.dumps({"project": project})

        # Set the query parameters for the project
        query = {"projectId": projectId}

        # Make the API request to add the component to the project
        response = requests.post(
            api_endpoint,
            data=payload,
            headers=headers,
            params=query,
            auth=auth,
        )

        if response.status_code == 201:
            print(f"Added component to '{project}' with name '{name}'.")
        else:
            print(
                f"Failed to add component to '{project}'. Status Code: {response.status_code}"
            )

All I get back is a 400 and I have no idea what's failing, or if my script is just fundamentally broken.

1 answer

0 votes
S
Contributor
February 19, 2025

For debugging API's I use PostMan

  1. From your python code you can grab the JSON that's getting created.
  2. Then go to PostMan set up Basic Authentication.
  3. Paste the URL and JSON in body.
  4. Error should be shown below PostmNa Setup_Workspace.png

API Problem_Dummy_My_Workspace.png

 

Hope this helps!

Gabriel Roth February 19, 2025

I'll see if I can get that to work. Postman doesn't really handle Python though, and the Jira API doesn't really allow you to construct looping JSON requests.

S
Contributor
February 19, 2025

how many rows in CSV?

Additionally between every API call you can consider putting a delay of ew seconds so that Jira doesn't feel your bombing it...

Gabriel Roth February 19, 2025

It will be a few dozen rows in the csv, I'm alternately getting either a 400 or a 404 error depending on what I tweak.

S
Contributor
February 19, 2025

Few things to establish

  • is it possible to share ur JSON that's 
  • Are you able to create a single ticket or thats also not working?
  • Check your create screen fields on all the projects and ensure that JSON matches to the fields there.
Gabriel Roth February 19, 2025

1. This isn't JSON, this is Python looping over a .csv

2. Not creating tickets, I'm creating a new, identical, component in about 4 dozen projects

3. Completely unrelated to screens or fields.

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events