logo

NJP

ServiceNow Scripted REST APIs: Part 11 – HTTP methods versus Javascript methods

Import · Mar 16, 2023 · video

foreign to part 11 in this series on creating scripted rest apis in servicenow in this video I'd like to talk briefly about HTTP methods versus JavaScript methods if you haven't looked at the previous videos in this series take a look at them because in them we create a new scripted rest API in servicenow and we add to that API various resources for getting deleting posting patching records in our vehicles table but one of the questions you may be asking yourself is well why is the HTTP method important because after all we're actually using JavaScript methods in particular the Glide record API to perform database operations that is the method that's the tool that we're using to change records create records and so forth so why do we need to select a HTTP method after all or HTTP is the protocol that's being used to exchange information between two different servers and you need to make sure that the method that you're using for JavaScript aligns to what you have for http I mean can you really put a Glide record query inside a post HTTP method request and can you put a Glide record insert operation into a get method will that work you could but there are a lot of things wrong with that scenario so yes JavaScript is used to act or is interacting with the table but the request itself has been transmitted by HTTP so again the client and the server are communicating over this protocol now there are at least three reasons that I can think of why the method that you use for HTTP is important for your scripted rest apis firstly server responses are standardized and are based on the HTTP method if you go to the documentation for this protocol you'll see that certain error codes have been defined for different methods and clients need and expect a standardized response to operations in order to understand what to do next so for example a HTTP code of 201 indicates that a record was successfully created you wouldn't expect this code as a client if you're performing a delete operation the second reason relates to item potency item potent methods refer to methods that when executed on the server result in the same state of the server every single time no matter how many times I perform that operation take the get query for example if we don't make any changes to the table itself we would expect that a get query with certain parameters will result in the same response every single time and on the database side there will be no change to those records at all why is this important well if a communication error were to occur how would the client respond what is it supposed to do next for a get operation and other item potent operations it's actually safe to just wait a little bit and send the request again because again it's not changing the state of the server however if we were performing a post operation and creating records on the server and the communication error code what should we do then should we just assume that it didn't work and we just send the request again that could actually result in duplicate records being created we don't actually know what has happened on the server side so these response codes are very important for the client to be able to determine what to do next the third reason that HTTP methods are important for your scripted rest apis is caching with item potent methods and operations you can form them as much as you like the state of the server will not change and then this allows a server to take those requests and cage them because what is being returned by the server will be the same socation will actually speed up the response times of the server so to summarize don't mix up your JavaScript and servicenow glide record methods with inappropriate HTTP methods use the corresponding HTTP method for the chosen operation don't confuse the server don't confuse the client and your users stick to the standard stick to what they know and you can't go wrong

View original source

https://www.youtube.com/watch?v=rij4CeyhdC0