Creating a New Resource
Creating a New Resource
One of the principles of REST is the use of simple and uniform interfaces.
In the REST API, new resources are created using the HTTP POST method.
Example: Create a Service Request
POST http://myserver:5181/odata/ServiceRequests HTTP/1.1
Content-Length: 247
Content-Type: application/json
{
'Description': 'Printer is not working properly.',
'Information': 'The printer in room 14C is causing paperjams all the time.',
'Medium': {
'Text': 'External',
'Value': 12783238
},
'Status': {
'Value': 3120234513,
'Text': 'Waiting'
},
'Impact': {
'Value': 3094610021
}
}
- Codes can be assigned by id value, localized text, or both.
- A resource must not contain a key during creation.
- All required fields must be provided.
The response will contain the created item, including values automatically assigned by the server:
{
"Description": "Printer is not working properly.",
"Information": "The printer in room 14C is causing paperjams all the time.",
"Status": {
"Text": "Waiting",
"Value": 3120234513
},
"Medium": {
"Text": "External",
"Value": 12783238
},
"Impact": {
"Text": "Low ( 1 person affected)",
"Value": 3094610021
}
}
Custom Codes
Custom codes require the @odata.type annotation:
{
"Description": "Printer is not working properly.",
"Information": "The printer in room 14C is causing paperjams all the time.",
"ServicecallCode1": {
"@odata.type": "#PROLIN.Web.OData.Model.Code",
"Text": "Code 1",
"Value": 3120234513
}
}
Entity References and Relationships
Entity references can be created with @odata.bind.
{
"Description": "Printer is not working properly.",
"Information": "The printer in room 14C is causing paperjams all the time.",
"Status": {
"Text": "Waiting",
"Value": 3120234513
},
'Caller@odata.bind': '/Persons(234)',
'Workorders@odata.bind': [
'http://myserver:5181/odata/Workorders(123)',
'http://myserver:5181/odata/Workorders(456)'
]
}
Returning Created Objects
When creating a new entity, you can add a Prefer header to request that the created object be returned.
Create Using a Template
You can create new entities using a template.
Templates define the structure, and additional values posted override template values.
Example: Create a Configuration Item using the ROUTER template
POST http://myserver:5181/odata/ConfigurationItems HTTP/1.1
Content-Type: application/json
{
‘Template@odata.bind’: ‘/Templates(3101753354)’,
‘Searchcode’: ‘RESTAPI’,
‘Name 1’: ‘REST API Router’,
‘IPAddress’ : ‘192.168.1.1’
}
The template is referenced by its Id.
To list available templates for a specific entity type:
GET http://myserver:5181/odata/Templates?$filter=Entity/Name eq '[Entity Name]'
Where [Entity Name] matches one of the supported entities.