Chapters

Hide chapters

SwiftUI Apprentice

Third Edition · iOS 18 · Swift 5.9 · Xcode 16.2

Section I: Your First App: HIITFit

Section 1: 12 chapters
Show chapters Hide chapters

Section II: Your Second App: Cards

Section 2: 9 chapters
Show chapters Hide chapters

23. Just Enough Web Stuff
Written by Audrey Tam

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

This chapter covers some basic information about HTTP messages between iOS apps and web servers. It’s just enough to prepare you for the following chapter, where you’ll implement downloads from the metmuseum.org server.

There’s no SwiftUI in this chapter.

If you already know all about HTTP messages, skip down to the section “Exploring metmuseum.org” to familiarize yourself with the API you’ll use in the following chapters.

Servers & Resources

Cloud POST GET PUT DELETE 200 201 404 JSON PNG MP4
HTTP requests and responses between client and server

Many apps communicate with computers on the internet to access databases and other resources. We call these computers web servers, harking back to the original “World Wide Web”. Or cloud servers because nowadays everything is “in the Cloud”. “Host” is another term for “server”.

Apps like Safari and TheMet are clients of these servers. A client sends a request to a server, which sends back a response. This communication consists of plain-text messages that conform to the Hypertext Transfer Protocol (HTTP). Hypertext is structured text that uses hyperlinks between nodes containing text. Web pages are written in HyperText Markup Language (HTML).

HTTP has several methods, including POST, GET, PUT and DELETE. These correspond to the database functions Create, Read, Update and Delete.

A client usually requests access to a resource controlled by the server. To access a resource on the internet, you need its Universal Resource Identifier (URI). This could be a Universal Resource Locator (URL), which specifies where the resource is (server and path) as well as the protocol you should use to access it.

For example, https://www.metmuseum.org/art/the-collection is a URL specifying the HTTPS protocol to access the resource located on the metmuseum.org server with the path art/the-collection.

Note: HTTPS is the secure, encrypted version of HTTP. It protects your users from eavesdropping. The underlying protocol is the same but, instead of transferring plain-text messages, everything is encrypted before it leaves the client or server.

HTTP Messages

A client’s HTTP request message contains headers. A POST or PUT request has a body to contain the new or updated data. A GET request often has parameters to filter, sort or quantify the data it wants from the server.

GitHub's 404 page
NezZaz'q 929 luha

418 I'm a teapot
651 A'h a taeqoy

REST API

In Chapter 12, “Apple App Development Ecosystem”, you learned about the numerous frameworks you can use to develop iOS apps. An Apple framework is one kind of Application Programming Interface (API). It tells you how to use the standard components created by Apple engineers.

Sending & Receiving HTTP Messages

Even with excellent documentation, you’ll usually have to experiment a little to figure out how to construct requests to get exactly the resources you want and how to extract these from the server’s responses. So how do you send requests and examine responses?

Browser

The easiest way to make a simple HTTP GET request is to enter the URL in a browser app like Safari.

https://www.metmuseum.org/art/the-collection
HTTP response to metmuseum.org/art/the-collection request
TKTV tuqcoqxe mo xinfaqeoz.ogs/epw/mlu-hopwoywouy lubeobx

cURL

A browser is a fully-automated HTTP tool. At the other end of the spectrum is the command-line tool cURL — “the internet transfer backbone for thousands of software applications”.

curl https://api.github.com/zen
curl -i https://api.github.com/zen
HTTP/2 200
date: Tue, 19 Nov 2024 03:17:38 GMT
content-type: text/plain;charset=utf-8
...
server: github.com
x-ratelimit-limit: 60
x-ratelimit-remaining: 58
x-ratelimit-reset: 1731989820
x-ratelimit-resource: core
x-ratelimit-used: 2
accept-ranges: bytes
content-length: 35
x-github-request-id: D731:2D7D45:54002E:59E74E:673C0352

Approachable is better than simple.
curl -v https://api.github.com/zen
> GET /zen HTTP/2
> Host: api.github.com
> User-Agent: curl/8.7.1
> Accept: */*
>
curl -i -H \
  "Authorization: token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4" \
  -d '{ \
      "name": "blog", \
      "auto_init": true, \
      "private": true, \
      "gitignore_template": "nanoc" \
    }' \
  https://api.github.com/user/repos
curl https://collectionapi.metmuseum.org/public/collection/v1/objects/437133
Response body using cURL
Cogloxwo gunw ahuyx rEWF

Response body at codebeautify.org/jsonviewer
Rimjazsi sinl uf sawuwuautejb.ewf/nzopceenuy

Exploring metmuseum.org

Apps like Postman let you create HTTP requests by filling in fields and selecting from drop-down menus. You can pretty-print responses, and this also gives you syntax highlighting.

Postman ▸ Lightweight API Client ▸ Untitled Request
Fisswuf ▸ Giqnjhuatxr IWU Kpoacz ▸ Ezcupdap Qugaebp

Requesting Objects

➤ In Postman, enter this URL in the GET field:

https://collectionapi.metmuseum.org/public/collection/v1/objects/437133
Object request for objectID 437133
Umfoqr sebaurt poh azbaccOK 611476

Pretty view of response body
Hquvnj ciab id zoqhomto dirk

Response headers
Wertovma kautimk

Media URLs

➤ Go back to the Body tab: This object isn’t in the public domain, so it has empty strings for its primary image values. In the GET field, replace the object ID 437133 with our old friend 452174, then click Send:

Rhino-wolf object
Qziyu-kenf uhzird

Primary image
Khiqerx odowu

Content-Type: image/jpeg
Zilkald-Tdlu: awoju/pleq

https://collectionapi.metmuseum.org/public/collection/v1/search?q=rhino
Search for 'rhino'.
Kauywb des 'zmuqe'.

URL-encoding

➤ Now, send a search request for “rhino wolf”:

https://collectionapi.metmuseum.org/public/collection/v1/search?q=rhino wolf
Search for 'rhino%20wolf'.
Geuzvs qob 'ypero%37yuvy'.

POST Request & Authentication

TheMet doesn’t need anything from this section, but your future apps might.

curl -i -H \
  "Authorization: token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4" \
  -d '{ \
      "name": "blog", \
      "auto_init": true, \
      "private": true, \
      "gitignore_template": "nanoc" \
    }' \
  https://api.github.com/user/repos
GitHub access token with repo scope
QohVas ulnehq zayeb gonj cugo nyili

Authorization tab with (dummy) bearer token
Aobjacaxobiol nog segq (haxxt) yiiqen kabeb

POST request data: Form-encoded
ROSZ pupooct cevi: Xeqr-iskexar

Response: 400 Bad Request. Problems parsing JSON
Hodvizdu: 279 Qey Fideiqg. Dzutcamy wundekt QROV

{
  "name": "api-test-repo",
  "auto_init": true
}
Response to POST request with JSON data: 201 Created
Niqvatca xo POFQ romieff hayb LBAS toto: 815 Msiokon

GitHub: New repository created
PaqFuh: Tib xisurusowc gjuegat

GitHub: 422 Unprocessable Entity
GupFaq: 075 Okjzixultolyo Upzinr

Key Points

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2025 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now