简体   繁体   中英

Not knowing where to start in using REST API for creating ServiceNow incidents

I need to update a webform which was used to create incident tickets with a SOAP API. Now it needs to be used with a REST API by ServiceNow and frankly speaking, I don't know where to start.

The existing webform works as follows: After filling out the form and pressing on the submit-button the "submit.js" is triggered. This function posts all the information in the webform to the "soapclient.php". This soapclient.php connects to the SOAP client and transfers all the data to the SOAP API.

The "soapclient.php" looks as follows:

<?php
    $wsdl =wsdl-server;
    $client = new SoapClient($wsdl, array('login' => "USER",
                                          'password' => "PW“,
                                          'trace'=> 1));

    $title = $_REQUEST['title'];
    $affuser = $_REQUEST['affuser'];
    $description = $_REQUEST['description'];
    $solutionstring = '';
    $solutioncode = '';
    $resolveimmediately = '';

 [[SEVERAL IF-ELSE STATEMENTS]]

    $request = array(
            'model'=>array(
                'keys'=>array(),
                'instance'=>array(
                    'registrationId' => ID,
                    'affectedUserId' => $affuser,
                    'serviceId' => $serviceId,
                    'affectedCiId' => '',
                    'priority' => '4',
                    'title' => $title,
                    'description' => "$description\n$timestring$errorstring$phonestring",
                    'resolveImmediately' => $resolveimmediately,
                    'solutionCode' => $solutioncode,
                    'solution' => $solutionstring
        )));

$response = $client->SubmitIntApiIncident($request);

I'm pretty sure that I only have to change the "soapclient.php" into using the new REST API instead of the SOAP API. But this is the part where I have NO idea where to start. The REST API uses an API key + a generic user (which I both have) - but I don't know WHERE to use them.

The only clue that I have is a swagger.json file I could download. This KINDA looks like the structure I'm looking for, but I have no idea how to use it.

The "swagger.json" looks as follows:

{
  "swagger" : "2.0",
  "host" : "send-dev.servicenow.com",
  "basePath" : "/api/ServiceNow/devb/incident/v2.5",
  "schemes" : [ "https" ],
  "paths" : {
    "/incident/createIncidentMethod" : {
      "post" : {
        "description" : "Create incident\n",
        "operationId" : "POST /incident/createIncidentMethod",
        "parameters" : [ {
          "description" : "Create Incident",
          "required" : false,
          "in" : "body",
          "name" : "body",
          "schema" : {
            "properties" : {
              "header" : {
                "properties" : {
                  "transactionid" : {
                    "type" : "string"
                  },
                  "sourcesystemid" : ""{
                    "type" : "string"
                  },
                  "targetsystemid" : " "{
                    "type" : "string"
                  }
                },
                "type" : "object"
              },
              "content" : {
                "properties" : {
                  "caller_id" : {
                    "type" : "string"
                  },
                  "category" : {
                    "type" : "string"
                  },
                  "subcategory" : {
                    "type" : "string"
                  },
                  "business_service" : {
                    "type" : "string"
                  },
                  "ci_name" : {
                    "type" : "string"
                  },
                  "impact" : {
                    "type" : "string"
                  },
                  "urgency" : {
                    "type" : "string"
                  },
                  "assignment_group" : {
                    "type" : "string"
                  },
                  "assigned_to" : {
                    "type" : "string"
                  },
                  "short_description" : {
                    "type" : "string"
                  },
                  "state" : {
                    "type" : "string"
                  },
                  "close_code" : {
                    "type" : "string"
                  },
                  "close_notes" : {
                    "type" : "string"
                  },
                  "service_offering" : {
                    "type" : "string"
                  },
                  "affected_user" : {
                    "type" : "string"
                  },
                  "description" : {
                    "type" : "string"
                  },
                  "correlation_id" : {
                    "type" : "string"
                  },
                  "ci_sysid" : {
                    "type" : "string"
                  }
                },
                "type" : "object"
              },
              "attachment" : {
                "properties" : {
                  "file_name" : {
                    "type" : "string"
                  },
                  "mime_type" : {
                    "type" : "string"
                  },
                  "base64string" : {
                    "type" : "string"
                  }
                },
                "type" : "object"
              }
            },
            "type" : "object"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "Success",
            "schema" : {
              "type" : "object"
            }
          },
          "401" : {
            "description" : "Not authorized",
            "schema" : {
              "type" : "object"
            }
          },
          "610" : {
            "description" : "User doesn't exist.",
            "schema" : {
              "type" : "object"
            }
          },
          "611" : {
            "description" : "Group doesn't exist.",
            "schema" : {
              "type" : "object"
            }
          },
          "612" : {
            "description" : "User is not member of the group.",
            "schema" : {
              "type" : "object"
            }
          },
          "613" : {
            "description" : "Incident State is invalid.",
            "schema" : {
              "type" : "object"
            }
          },
          "405" : {
            "description" : "Not supported. Invalid parameters.",
            "schema" : {
              "type" : "object"
            }
          },
          "603" : {
            "description" : "Missing mandatory information.",
            "schema" : {
              "type" : "object"
            }
          },
          "614" : {
            "description" : "Incident Category is invalid.",
            "schema" : {
              "type" : "object"
            }
          },
          "615" : {
            "description" : "Incident Subcategory is invalid.",
            "schema" : {
              "type" : "object"
            }
          },
          "616" : {
            "description" : "Only one input is allowed. Configuration item Name or SysId.",
            "schema" : {
              "type" : "object"
            }
          },
          "606" : {
            "description" : "Record not found.",
            "schema" : {
              "type" : "object"
            }
          },
          "618" : {
            "description" : "CI name is not unique."
          },
          "608" : {
            "description" : "Impact is invalid. Impact must be 1, 2 or 3.",
            "schema" : {
              "type" : "object"
            }
          },
          "609" : {
            "description" : "Urgency is invalid. Urgency must be 1, 2 or 3.",
            "schema" : {
              "type" : "object"
            }
          }
        }
      }
    }
  },
  "info" : {
    "title" : "ServiceNow Incident v2.5",
    "description" : "This REST API provides methods to create/update/retrieve ITSM incident module data from ServiceNow. This API uses Basic authentication (plus API Key). If there is a need to consume from outside the network, there is another version of this API that is configured for two factor authentication.",
    "version" : "1.0.0",
    "x-summary" : "SNOW Incident API"
  }
}

I would be incredibly grateful if anyone could give me a hint on how to use the new API or how to change the soapclient.php to work with the new API.

Best Tim

Your swagger.json is not valid JSON (however it has small bugs easy to remove - on line 23 and 26 remove " " before brace). You can view that file in https://editor.swagger.io/ . That file describe only one request:

POST http://send-dev.servicenow.com/api/ServiceNow/devb/incident/v2.5/incident/createIncidentMethod

To send request (POST with proper json in body) to that API form php you can use GUZZLE library instead SoapClient (guzzle has good documentation).

In swagger.json you have only following information:

This REST API provides methods to create/update/retrieve ITSM incident module data from ServiceNow. This API uses Basic authentication (plus API Key). If there is a need to consume from outside the network, there is another version of this API that is configured for two factor authentication.

but without details - however it is used key word "Basic Authentication" . So you can try to add following header to your POST request:

Authorization: Basic username:password_base64

Where username:password_base64 is your credentials code by base64 - as example for user demo and pass p@55w0rd you need to code base64 string "demo:p@55w0rd" and to add header:

Authorization: Basic ZGVtbzpwQDU1dzByZA== 

(it should be use with http/ssl because base64 is easy to decode). If this will not work, then you need to ask your API provider: how to "login" to that API (some API need only additional path parameter like `?key=abc...xyz').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM