Jira REST API Create Worklog returns 200

Hello, I’m new to this forum and new to creating automations related to JIRA. I think it’s something simple, but I can’t figure it out. I need to create a simple Python script that automates worklog to an issue. I’m using the WorklogPRO plugin and this is the script I’m using:

import requests
import json
from datetime import datetime

# Jira credentials and URL
jira_url = "http://jira.mycompany.com"
api_token = "***"
email = "myemail@mycompany.com"

# Worklog details
issue_key = "AP-123"
time_spent = "1h"  # Time spent on the issue
comment = "Test"
started = datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000+0000")  # Current time in Jira format
time_spent_seconds = 3600  # Time spent in seconds

# Endpoint for adding worklog
worklog_url = f"{jira_url}/rest/api/3/issue/{issue_key}/worklog"

# Worklog data
worklog_data = {
    "timeSpent": time_spent,
    "comment": comment,
    "started": started,
    "timeSpentSeconds": time_spent_seconds,
}

# Headers for the request
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_token}"
}

# Make the request to add worklog
response = requests.post(
    worklog_url,
    headers=headers,
    data=json.dumps(worklog_data)
)

# Check the response status
if response.status_code == 201:
    print("Worklog added successfully.")
else:
    print(f"Failed to add worklog. Status code: {response.status_code}")

I’m encountering a problem where the script correctly connects to JIRA, but it always returns status code 200 instead of 201. I can’t figure out where it goes wrong. I know there are some answers to similar questions on the internet, but I can’t find any solution that I could use for my problem. I guess that somehow script uses a GET request instead of POST but I can’t figure out where.

I would be very grateful for any help. Best regards.

I just want to double check you’re using Jira Cloud, because that jira_url looks a little like a Jira Data Centre URL - obviously it’s just an example, but if you are using JIRA server then you’ll need to use a different Rest API.

That being said, I think the issue is that you need to include the request type when you make the request to add your worklog. I’d suggest you try:

response = requests.post(
    "POST",
    worklog_url,
    headers=headers,
    data=json.dumps(worklog_data)
)

If you pop over to the API docs for Add worklog, to the right you’ll see some sample code and you can select Python for some example code which can help work out where you’re going wrong.

If this resolves the issue, be sure to mark it as the solution. If you’re still getting stuck please reply to the thread with more information!

Cheers,
Mel