POST method not supported for API

Alex153
Tera Contributor

Hi,

I am trying to create a table via API and custom fields. I am succeeded  to create a table but failed to add custom fields. I got an error 

Failed to create field field234. Status code: 405, Response: {"error":{"message":"Method not Supported","detail":"POST method not supported for API"},"status":"failure"}

 that's my script (using python)

import requests
import csv

url = 'https://instance.service-now.com'  
table_name = 'u_new_table_20'
username = 'username'
password = 'password'

csv_file = r'C:\Users\PycharmProjects\pythonProject\table_names.csv' 
fields_to_add = []

with open(csv_file, 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
        field_type = row['Field type']
        column_label = row['Column Label']
        element = row['Element'] 
        field_name = row['Field name'] 
        fields_to_add.append({
            'name': field_name,
            'element': element,
            'type': field_type,
            'reference': table_name,
            'label': column_label
        })

auth = (username, password)

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

for field_data in fields_to_add:
    response_field = requests.post(
        f'{url}/api/now/v1/table/{table_name}/sys_dictionary',
        headers=headers,
        auth=auth,
        json=field_data
    )
    if response_field.status_code == 201:
        print(f'Field {field_data["label"]} has been created successfully.')
    else:
        print(f'Failed to create field {field_data["label"]}. Status code: {response_field.status_code}, Response: {response_field.text}')

 

Would like to know if there is a chance to add fields via API or it is useless?

1 ACCEPTED SOLUTION

Fields are especially bad yes. 

Actually, all records which are normally captured in an update set, should never be created through an API, because you will go out of sync between your instances in no time

 

When a field creation goes wrong you might end up with a dictionary entry which cannot be used, not be updated and if you're unlucky, not be deleted.

I've been there before, luckily, I'm wiser now. 😉


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

5 REPLIES 5

Peter Bodelier
Giga Sage

Hi @Alex153,

 

I would highly discourage creating configuration files like dictionary entries through an API. (If its possible at all).

 

You will be out of sync between your instances in no time. And probably will not be able to use the fields correctly.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Well, even when I get "field_data["label"]} has been created successfully" nothing appeared in the table. So if you can advise what else to check would highly appreciate it.

Hi @Alex153,

 

Normally I wouldn't decline giving help. But you really shouldn't do this. So I will not help to get it done, whilst I advice against it.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

If I understand it right it is a bad idea in general to create fields via API?