Web Services

Oct 27, 2022

Web Services

Web Service

Web Services are a method for two services to communicate to one another

Web services are typically not intended for direct human consumption and messages are usually constructed in data formats like XML or JSON to ease consumption by a program

Web services expose an API that can be consumed by other programs

Web Service

There are many different standard protocols for web services

The main ones are SOAP and REST

We’ll focus on REST, as it is a bit simpler to use and maps to HTTP verbs we have already learned about

REST

REST (Representational State Transfer) – standardizes interactions with web services through already existing HTTP verbs (GET, POST, PUT, DELETE); other protocols (e.g. SOAP) will create their own arbitrary methods

  • GET – Used to list or retrieve records
  • POST – Used to create a record
  • PUT – Used to update/replace a record
  • DELETE – Used to delete a record

CRUD

CRUD Application – a generic term for an application that is mostly involved in Creating, Reading, Updating, and Deleting records

The HTTP verbs map well to this archetype:

  • Create – POST
  • Read – GET
  • Update – PUT
  • Delete - DELETE

JSON

JSON (JavaScript Object Notation) – standard data format that is human readable while also being parseable by JavaScript interpreters

An alternative to XML

Many web services give you the choice of using JSON or XML data

JSON Example

{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 25,
  "height_cm": 167.64,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    { "type": "home", "number": "212 555-1234" },
    { "type": "fax", "number": "646 555-4567" }
  ]
}

todos [users](https://gorest.co.in/public/v2/users building