How to add an option to a select list using REST API in Python?

Okay I am looking at How can I add options to a Select List custom field via Rest Api?, but I am still running into an issue.

I am trying to use a python script to create the single issue selection list, then I want to add pick values to the default context, as well as other contexts.

This command is asking me for the contextId of the default context. Where do I find that?

Okay: Edited to add… I think I got past that part
I used: url = “https://your-domain.atlassian.net/rest/api/3/field/{fieldId}/context

Now I am trying to add the options, and the sample python code doesn’t seem to be working. When I add my list of options in I get the really helpful error of “Invalid request payload. Refer to the REST API documentation and try again.”

I even tried copying the sample piece directly into my code… same error

Welcome to the Atlassian developer community @SteveLundy,

I think that error message indicates that you do have the right HTTP method, URL, and auth. Let’s focus on the payload. Can you log that out from your Python code and let us know the JSON that is getting sent?

Hi,

Thanks in advance for your assistance.

Here is the payload that I am trying to load into my field. These should be all selectable options for my single value select list.

payload = json.dumps( {
  "options": [
    {
      "disabled": False,
      "optionId": "10001",
      "value": "Blue Light Drop-In Filter"
    },
    {
      "disabled": False,
      "optionId": "10001",
      "value": "1788 CCU"
    },
    {
      "disabled": False,
      "optionId": "10001",
     "value": "1788 Microscope CH"
    },
    {
      "disabled": False,
      "optionId": "10001",
     "value": "1788 LC Shutter Pendulum CH"
    },
    {
      "disabled": False,
      "optionId": "10001",
      "value": "1788 Pendulum CH (non-LC Shutter)"
    },
    {
      "disabled": False,
      "optionId": "10001",
     "value": "1788 Integrated CH"
    },
    {
      "disabled": False,
      "optionId": "10001",
      "value": "1788 Inline CH"
    },
    {
      "disabled": False,
      "optionId": "10001",
      "value": "1788 Standard CH"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1788 Camera Cable Rev P4"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1788 Camera Cable Rev P3"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1788 Camera Cable Rev P2"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1788 Camera Head Rev P3"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1788 Camera Head Rev P2"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1788 Main Board Rev P3"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1788 Main Board Rev P2"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1788 Digital Board Rev P3"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1788 Digital Board Rev P2"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1788 CCU Rev P3"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1788 CCU Rev P2"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1688410110 Rev A - AIM 4K AUTOCLAVABLE COUPLER, 20MM, C-MOUNT"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1688410105 Rev P2 - PKG, 1688 AIM 4K AUTOCLAVABLE CAMERA HEAD"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1688410105 Rev A - PKG, 1688 AIM 4K AUTOCLAVABLE CAMERA HEAD"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "1688410110 Rev P2 - AIM 4K AUTOCLAVABLE COUPLER, 20MM, C-MOUNT"
    },
    {
      "disabled": True,
      "optionId": "10001",
      "value": "Charger"
    }
  ]
} )

I have tried several iterations of this payload. One where I number all of the option ids sequentially from 10001 to 10024, one where I leave the optionID out all together, one where I attempted to create optionIds in a different call… no joy.

If you have any suggestions, please let me know.

Regards,

Steve

@SteveLundy,

Are you using PUT or POST? I’m pretty sure the docs for POST lie (kind of, depending). When you create a cascading option field, then the optionId is meant as a reference for the parent option. If you have a flat option list, you should not send an optionId. And, I suspect that PUT only allows you to update existing optionIds and might fail if those ids don’t exist. I’m pretty sure these will all report different errors, so could you please report the HTTP status & full error message JSON? I just want to be sure we’re chasing the right problem.

I know you said you tried leaving out optionId already but can you try this approach again, making sure to use POST. Also, overall, lets try to get this to a simpler test case. Can we remove the optionIds and also reduce the array to 1 option element? Once we get the simple thing working, it’s easier to add more back again.

Okay, that did the trick: I updated the code as follows:

url = "https://XXXXXXXXX-sandbox-XXX.atlassian.net/rest/api/3/field/" + New_fieldID + "/context/" + New_DefaultContextID + "/option"

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

payload = json.dumps( {
  "options": [
    {
      "disabled": False,
      "value": "Blue Light Drop-In Filter"
    },
    {
      "disabled": False,
      "value": "1788 CCU"
    }

  ]
} )


response3 = requests.request(
   "POST",
   url,
   data=payload,
   headers=headers,
   auth=auth
)

Once I switched out the PUT to a POST it started to work.

Thanks for your assistance.

Regards,

Steve

This is just a little moan - but i wish Atlassian api capability was the same in DataCentre/Server as it is in cloud + be so cool if answers could inc server too sometimes - Since the great move to cloud. DC is a somewhat forgotten beast despite the fact that we still pay $100,000’s. Some would say its almost deliberate?!?!?

1 Like