1 of 28

Client-Server Communication over HTTP

CSCI 338: Software Engineering

Fall 2025

1

2 of 28

Agenda

  • Intro to Web Services and Web APIs
  • Intro to HTTP
  • Interacting with a REST APIs
  • Intro to the JavaScript fetch API
  • Summary

2

3 of 28

Outline

  1. Intro to Web Services and Web APIs
  2. Intro to HTTP
  3. Interacting with a REST APIs
  4. Intro to the JavaScript fetch API
  5. Summary

4 of 28

Understanding how to work with Web Services & APIs is so useful!

  • Many organizations make their data, storage, and compute services available over the web.
  • Understanding how to interact with various web services using the HTTP protocol is perhaps one of the most useful, practical skills that you can learn.
  • This isn’t just relevant for web browsers – all modern programming languages have a way to encode, decode, and communicate messages over HTTP.

5 of 28

What is a Web API?

API → “Application Programming Interface”

A set of rules that you have to follow to interact with a piece of software

Web API

An API that you access via HTTP protocol. Offers a systematic way to:

  1. Create update, read, and delete resources over the internet
  2. Expose parts of your database to third party clients
  3. Allow a web client (or any client) to take advantage of resources distributed across providers, servers, organizations, etc.

REST API

A kind of Web API that honors the “REST” endpoint design conventions

5

6 of 28

Many Organizations Publish their Data via Web APIs

  • Companies:
    • e.g., Spotify, Yelp, OpenAI, etc.
  • Government Organizations:
    • e.g., NASA, Asheville Open Data Portal, etc.

7 of 28

Outline

  1. Project 1 Feedback
  2. Intro to Web Services and Web APIs
  3. Intro to HTTP
  4. Interacting with Web / REST APIs
  5. Intro to the JavaScript fetch API
  6. Summary

8 of 28

HTTP: Hypertext Transfer Protocol

Web servers communicate through HTTP – a stateless protocol for encoding, decoding, and transmitting messages over the web.

  • Stateless mean that no memory is preserved between requests – each request must contain all the information needed on its own to fulfill the request (no stored context).
  • HTTP requests and responses have a header (which contains metadata about the request) and a body (which contains the request itself)
  • HTTP requests also have methods (GET, POST, PATCH, PUT, DELETE, etc.) – next slide.
  • HTTP responses have status codes that tell you whether the request was successful or not.

8

9 of 28

HTTP Methods

Methods tell the server what action they should take (verbs):

9

Method

Job

Description

GET

Read

Retrieves / reads a resource (or container of resources) from an address (endpoint) on a server

POST

Create

Creates a NEW resource on a server; done by posting data to an address (endpoint) on a server

PUT

Replace

Replaces a resource on a server

PATCH

Update

Updates a resource on a server

DELETE

Deletes

Deletes a resource on a server

10 of 28

HTTP Status Codes

Status codes provide information re: whether your request was successful. Here is a list of them. Ones you should be familiar with:

Code

Definition

201

POST method successfully created a resource

200

Valid request made (generic success message)

400

Client posted a bad request that the server can’t understand

403

Forbidden (you don’t have access to it)

404

Resource cannot be found

500

Server error (usually because there’s a server bug)

11 of 28

Outline

  1. Intro to Web Services and Web APIs
  2. Intro to HTTP
  3. Interacting with Web / REST APIs
  4. Intro to the JavaScript fetch API
  5. Summary

12 of 28

Interacting with a Web / REST API: Clients

You can access a REST API via many different clients (including):

  1. Through your browser
  2. Through Java, C, C++, Python, or any other language
    1. In other words, servers talk to other servers via HTTP/S requests
  3. cURL or wget (command line tools)
  4. Postman
  5. And more!

13 of 28

13

PostgreSQL

FastAPI (python service) +�SQLAlchemy �Listening on port 80 / 443

Client Machine

(phone, laptop, tablet)

Curl

Python Script

Web �Browser

Public Address, Allows Connections From Anywhere

VIEW

MODEL + CONTROLLER

DATABASE

14 of 28

Demo �class-exercises-fall2025/fastapi_demo

14

15 of 28

Sync Fork, Pull Down, Make a new Branch

  • Sync fork to pull latest changes
  • If applicable: stage and commit your current work on your current branch
  • Checkout main
  • pull
  • Create a new branch called fastapi-exercise-b
  • Open the fastapi_demo folder in your code editor

15

16 of 28

Exercises: Python Client

  1. On your command line, navigate to fastapi_demo/ui/samples
  2. Let’s start with python:
    • cd into samples_python
    • Run poetry install
    • Open 01_create.py
    • Change the root url to my server (instead of localhost):�https://bc990ec75ec0.ngrok-free.app/docs
    • Run the script: poetry run python 01_create.py
    • What happened?

16

17 of 28

Exercises: cURL

  • On your command line, navigate to fastapi_demo/ui/samples
  • Let’s start with python:
    • cd into samples_bash
    • Open 01_create.sh
    • Change the root url to my server (instead of localhost):�____________________________
    • Run the script: bash 01_create.sh
    • What happened?

17

18 of 28

Exercises: JavaScript Client

  • On your command line, navigate to fastapi_demo/ui/samples
  • Let’s start with python:
    • cd into samples_javascript
    • Open 01_create.js
    • Change the root url to my server (instead of localhost):�____________________________
    • Run the script: node 01_create.js
    • What happened?

18

19 of 28

20 of 28

Outline

  1. Intro to Web Services and Web APIs
  2. Intro to HTTP
  3. Interacting with a REST APIs
  4. Intro to the JavaScript fetch API
  5. Summary

21 of 28

Intro to Fetch

JavaScript’s Fetch API

  • The Fetch API is a newer API for browsers to communicate asynchronously with servers communication (improving upon AJAX – the older way)
  • Because this API allows for asynchronous, non-blocking communication with servers, promises were introduced to the JavaScript Language.
  • Promises are objects that help programmers manage asynchronous communications.

21

22 of 28

What is a Promise?

  1. Promises exist to handle asynchronous functionality
  2. If you request a resource the cloud (i.e., someone else’s computer that lives somewhere else), you don’t know how long it’s going to take to process the request.
    1. Slow machine?
    2. Network traffic?
    3. Patchy WIFI!
  3. To handle this, a “promise” allows you to execute a piece of functionality only after the request completes.
    • It’s like putting a process on the “back burner” until it completes…and in the meantime your browser is free to do other things.
    • Uses the async / await keyword pairs.

23 of 28

Most requests happen in 2 Parts (w/2 Promises)

async function getData () {

const response = await fetch(url);

const data = await response.json();

}

  • fetch() returns a promise containing the response (a Response object). This is just an HTTP response, not the actual content.
  • To extract the body content from the response, we use one of the response’s content methods – in this case the json() method – which also returns a promise with the actual data.

24 of 28

Outline

  1. Intro to Web Services and Web APIs
  2. Intro to HTTP
  3. Interacting with a REST APIs
  4. Intro to the JavaScript fetch API
  5. Summary

25 of 28

Summary (1 of 2)

  1. Web services / REST APIs allow an organization to make their data, storage, and computing services available over the web via a web server.
    1. You interact with them via HTTP requests that specify actions via methods (GET=read, POST=create, PATCH=update, PUT=replace, DELETE=delete, etc.)
    2. You often need to authenticate with them by including an API key in the header of the HTTP request.
    3. Every web service is different – you need to read the provider’s documentation to understand how to use their service.
    4. Lots of different clients can make HTTP requests (e.g., web browser, most programming languages, PostMan, the command line, etc.)

26 of 28

Summary (2 of 2)

  1. You can interacting with a Web service in most modern programming languages
  2. In JavaScript, you can interact with a server by using the browser’s fetch API and promises.
    1. The fetch API allows you to issue HTTP requests
    2. Promises allow you to be able to handle asynchronous requests
    3. The async / await keywords allow you to create functions that can pause code execution until the promise resolves.

27 of 28

Activity: Integration with React

27

28 of 28

React + Data

Set-up:

  1. Navigate into fastapi_demo/ui
  2. Install the dependencies: npm install
  3. Start the bundler / hot reloading service: npm run dev
  4. Navigate to http://localhost:5173/

Check out the ui/src directory and take a look at the components

  1. Import the Todos component into App and invoke it (by adding a <Todos /> tag).

28