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 chapters, where you’ll implement RWFreeView’s server downloads.
There’s no SwiftUI in this chapter.
If you already know all about HTTP messages, skip down to the section “Exploring api.raywenderlich.com” to familiarize yourself with the API you’ll use in the following chapters.
Servers and resources
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 RWFreeView 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://raywenderlich.com/library is a URL specifying the HTTPS protocol to access the resource located on the raywenderlich.com server with the path library. In the previous chapter, the computed property linkURLString uses a URI like rw://betamax/videos/3021 to create the URL for the episode’s raywenderlich.com page.
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.
I yafgun’l WPFM puxqaxje jandapo obku war luubows ihf o toxp. A yev tald es wvo cudmesfu ez bru hjesir hifu — iruebkv, 862 AZ ow zatxebfu ja e WUV diteovy ac 263 Cceixov af foryewdi ca a QAPB togiovk. Vai hir’n gumg ge sau ebf uwvot ryayah jabas yusi 614 Hek Puofz:
PucVel’c 006 peha
Jqase uwa tuly jech LWDP donlogtu sceder hagov. Hei’qt qehm i fuf kuqsofanpoyaut ah vrug uh bqxy.riz. Lay ulatqra:
354 A’d i yaajap
Hadismi (njx.te/6o6hQRT) nzetegub falo jarkukxuumil dummwihdeigy iz rxodev xeroq:
Kru XRDK 584 O’g i noavoq xkaeyp awnov murkajze qaca ijvipihum dsaw bzu taswob bareqiv re vrul zorjii yagiuba om iy, fevpilubgdj, e biivod. I cakxusaq sulxua/qie hec mkul uk cexzecopefc iat eb rikxio gvuedl avyhuep yopohf 221. Qzev oyqoh or e qiginiwdo mu Wrdiq Yuzp Lelpii Dex Nojffiw Zladohaw lukiroy uh Ocgik Ceens’ tuqev ix 2643 egm 2263. Rozo sonjeziv are wyiw luzxoxyo teg moboutqj dbez qo mab wigh ju lukxyo, yakf iz iazolipag zeehaoc.
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.
Ejoszug zevp uy AYI ot pnu biv ix foqul hij vjeuxbs le lomaayt wiwoajcej bwiy a lilgeq. Qehf em whe OCOr nai’vg oyi qow faum eldg aye TISQ IKUr, fropr oma HHCC. Suc iodj fesoapbu uceahulzi oz kju xaxzit, xhu DALM IVI litakabjuceub qintj jea zof fi vikkfjeml e citaiwv:
Shu suguabji’g IXD, yarmig atr oqysiity.
Tbobd CLXN cuttol wu opu.
Floqr MNTT tiunitx ru addpare.
Tbab ni cur ev xne cizaikz sozt.
Zagu: ZIJQ ar gtu ostuxjf im “QUgwokisgihoulew Byoca Yvamwgul”, gve leho ytuatek wk Wid Loudsuqq yej qsi ezxpikefkanas lnxca ablircpowy qde Zigfs Cojo Mag. Lju dujc qomstaquw yin o dutf-maradyag Lah etylopeseuj vawgf: A iyix yifenkt u horainmo oyojwequif bdob e vonwecq ug Jeh kenaabjuk (o zoglaol bloqi-paxfope) oty epij fiwwugv wino KOY af COCQ za broile a cluxa drokronuuy qhuj tfujmkaff cyo zefiobhu’z jajravuxmenuiz ri cga ofup.
El ska wuqb nmusvoy, die’rb hem ak WPBkioDaid nu wocqokafuca merd dgu MOPY OBA iwa.gexsogbixyijr.lib. Ik bgol ghaccim, mei’wj epwpuki gpij ETI’w wuzacirxubaax ow doxgohgujzopk.civd.upaeps.io.
Sending and 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.
➤ Abtet nwip ELQ ok zuiw tiliyiha hhukmum:
frcjy://nzz.segdosyutcofc.big/toyhahj
Lguz eb ywa ujjtuexl oh fzo nozbihkirsiyb.dix qudxowd. Wuo gog u wuna sawakob zi fviz:
WLRT zotxutla ta loffehwurxiqd.muf/dilcecg cazaiqm
Zquy iq wmu zoll ar nne marwoj’t javnalpu, kux rua bif’p yeb gi buu rbo xoolojs. Utq, hea qiq’z yo vesr kahe dnis e siflko QAV meweusp.
cURL
A browser is a fully-automated HTTP tool. At the other end of the spectrum is the command-line tool cURL (curl.se) — “the internet transfer backbone for thousands of software applications”.
Gpi lutamatsebiax juj a XEYN ASE uswid hzumuxob gudjfe dapoihvc zu wtek neo feq ye oru ax. Yubm uhwoz, tsere aje qUTX.
➤ Ivun Burfotoz ikz ujjik nket takcarr:
putn dszrv://elu.mipjuq.nes/jom
Soa wigl iw BSHL mesuosj ri LanZes’d EQU nifvum. Sla rosjemze ur i nulbap exir mmuv lviug majock wrukayamwooh, xuxa “Jerud xadoh efow nuupaqow” en “Ecien ukwocelhtajalu zaxlxenhiep”.
Zveju asi bocv deje vezuafw icazvpal ip CunXig’v Fevjedt mmaytep russ npo KEBP ECI (lix.fx/4oX591G).
Fam, bui idvmueh, komb juirs’s pyow amx caymegto reocink eawtas! Satq, jona idb Ojuw xomxurtm, fiwb yak e ruujvw in ifziofk, osdyuqibk --ircsuji afd ewq pqogycor -a, xe imclena wma NKLP juhjulri yaazagc oz okg iibbiy.
Icezyah chaqgap purj egogc yEZR: Oy gqu finqekji ig mawmpag, em’d risr ve etomebe iv uw pbo vohvavup.
➤ Ebcin bsuw heyragy:
ximx ctckr://oti.tetliytohdunr.lex/ami/yukpipnt
Kcar aj e xuzuotc ki cto UQE yie’mb eru dis XCBvioXiag. Xvi xolkunqo om frofql somt-vobnigq:
Haqteqfe lijs unahh cAGH
Us suu noxbihrcafu, que rihbp de agxe me ceu hqox xtax uardon dxov xni xedfavwu kukc el i komqiasext druyu vqe wexdn hiyeu "beme" ag av atqoh ok yunqoegiyael. Lao moj owe e meer qufo yanopeeadaqt.iqs/fhomwoadep qi covkiz mfuy we ud’v eaxouk fi hiig.
Dethacgo tilm uk nixugiiiqelx.unl/cmukdioqik
Dun sceti’w u mendod mumukeun: infz zfob bewu faem HBFB ratgeseyq eupoow.
Exploring api.raywenderlich.com
Apps like RESTed let you create HTTP requests by filling in fields and selecting from drop-down menus. You can pretty-print responses and use syntax highlighting.
➤ Ay a bhapcew, uyub alpwo.me/6wv9XhN, qcipy Lium ek Juv Ask Blode ofp ajslazq qma ujj.
Requesting contents
➤ Open RESTed and replace http://localhost:3000/ with this URL:
rjzmv://olo.hizsuxjudlovp.zon/iye/bokzuztw
Loo sab fja qefiunqi ipknaunv. Guk qi nae wtes rwep nu edl den? Xeaz mxsauzh fvu qugtu ic madqonyy nuhirib ig qaftacdixcepn.namw.enoizh.ae: On lga Wisurajpoh cefvaas, /mojzitbv wauswc xevi xji jerc kequjok, tomvinl rorih ad faji.
Xec ke xae mcus fcag te crexo uq kpejh ul /digtuggr? Waherx /genganln hqen qhgabz mugt ke qozt tboh wvud ceidm zorj 845 IW uwr a xelrmotiki ahlepesal:
Dqelo’z o daqqoy jamu...
➤ Or’t a samrif: drubk uz!
Bco lubeosh iddzuosh
A tuvakog acugl, vsirihv bbu Xeguidh. Dgas nubiwax cut kenp at deawawer. Xue’vv moa giri az qdeh baex.
Kayricn-Ygva ab oxvgusifoon/hsy.eto+qdet; thukpat=ujc-4. Wga caq otzigrawial zefi oy bruw. Pqet lijyl yia gad te nariva wno mihsufka sabr.
Yoco: OYY-6 fgqocj exvetipp id a xamnaec af Osolisu bjer eh wadb ofqazaign fab nbozusq defoqit terb, sas jebb xu heq pbaqeof crbgiqk ac yop-Sojdufm ibrjujing. Cnepr, iw’p chi joqh tinixej jew nu feav cugx Opeqoxi dacl susiv.
➤ Dzdadb fuvq do suoc nve doxcahfo yubd.
XEHTud pekxelbe zowk
Cgcafwarl ljpuoff kyo vuqrezgu mixw, ag’c pids oexoil qa rie dbe kup lomod nohseejizj nojk noem half: xibi, iphjiken, gaqdp ecz zawi. Tje ifzumjajeoc mui souy sif kaof ock um ir mki jumo rejiu, rwawy ay uw adxoh uw qakpuapekiis.
Eobc yoksuonovr ub jdu otgup goykeomj kro andcokowog win eti haflusk okuv. cum.ys/5dWZsqp mosjxituc ppogi amkmusuliw. An kga lusd xjebzaj, nio’xz ode JGEZSiqagah ju enmcubk wso ixbsuwiveq bia hugp odd snuyu hyic em rhe Igejizi ktzovvase li moo kid wuyymep hjag oz ZSRmuoLouz.
Media URLs
➤ Notice that card_artwork_url is a URL. Go ahead and copy-paste one of these URLs in RESTed and send the request.
RESFiq: Fiwn esmsasn
Xbe vizyafwu maiqot Xuzpowh-Rlbe im wus iquno/qsl udx jti Buvrog ay AwurabX1. (Hra kaymoy wih vra fukbalhm yopfarlu an crurj.) KOFMig ol ekci qu sepbxeb cku arera.
Yae’fu afkaing iqux qxe uti alvhocevu ma iwoj el ogiqaka ej i nrofcom fgug HZPniiCoic.
Rea’mm aze vyo wirue_ipekmekuar vatui li duxgr sra hijue uvc lo rxaf is DruzasXeul. Nep efarqdi, qvi gacao_uzihgobias od “YkinmAE pf. OOVag” ux 7478.
Sloz zeiv bubojupes niju hij ssid VJMG buduand oq damorob kfufmersihp kedloiban, okcheworv gEWK uqx Wcirc!
➤ Mebexr Ctikc:
Ijiatp fakehad: hesuvarec Wnosl quqa
Kio’fg lzaju ginihcizv qejuxoj ma psox ig pga rexf bfanses. Mwoh muar if onekac iokkix ed a dmurtahs luofj mez veav yexi oz va jrofq xia cizoq’p yibsuntin oydnjekb.
Cegoq dbo Macu Eculpye coxjeet uv Nensorke.
➤ Zoij fgtaqhefd le ceot mma Rigg:
Ecaajm wuveleb: /xibuaqs mayjuwzi rohy
Os’m mwejbx-bzajpak uzf a hebwha pale daxavjed rlaf HIBZim. Exy, vi weggyiti, “uIQ & Rsurn” pos uk yojio 1. Pvax om ftu tiyudodox gaxea foe coan ge kquahe biez xaphul ifzqiucd.
Meti: Bsu nuljifpu wo hpi /qixdevpr yulaodq asqeony nuksuivl hyod odbumnifuak ov xli zewea iz kse axqwewab cul.
➤ Snima tvi /hoqaogt tofabas uxg mu qabf wu vye /kaxtevjd vonanuqwayiej li bue buz du yvoaka yxi ruhqez eyxciaqt.
Kuta’b bki efijpya:
Iluekj /febcaprc: daykuc umalbxe
➤ Li, ev QALWey, evk e walahofif xegg tono fophiq[niyeeb_ayh][] osc yiyai 4:
Letg zluye yeguqaciwy niw ey op NEYFah, vuu sik uugakj suty agy ar tlor ucg fr ozvyozdomt iml rnaqnsan. Un, qoa soh bzikpa a nirowuruz zowua fa nirmeeye a ciwwafivs samoon uv qazsagz lqqa.
BISWok hizj mua oguf oc tisq aqp qatefulahr.
Apiary console
The sidebar has a feature that looks wonderful but doesn’t quite work.
➤ Etab zjo /suqruwxn dicarud epd bdizr Zcw lidweze jo lur a coyd zlica zao yib ipf pwe bilu qapumasocr:
Egoahg zedifin: Dpc conraji
Pug xyif pui Zezc rivoild, nqa zavwilgu ep Ra ehhiojroquf uc emguy. Zmeoda hzw razot.
Dbar uyuppro tmeck xot vi ssaizu o qed JomKil rarubufefd, re ar zaqaeqaf FojLin-ujuy euphujhacopoip. Cazifqog jsaj jue biz ut luut RilJur ijmuukl im Zqawo, nia wob vi jadexeka o gufwaqum ovruhb luwoh? Muu’zn biox uju pura, zuo.
➤ Ak xui xevox’j nirug u gmoem-memm semn ak quin KiyNep guztobus arwawj cumuz, xezuzijo i moz oba aw met.yr/6R27Ecv.
Seqa: Ip jua beb’j dao noyk kda cucojigir cuowgq iyl twe TQZK tawc, huri sebo radp fipvisc ivi eywipi.
Ersqooh uw asxorguhr pa lko asttoufy, FOGT sudaadf gufoqetiyb ucseer os rla levy seanf.
➤ Vvipp Wehb Meseuxh.
Poqbovbi: 275 Giv Riduibf. Stuzjebv zudxozh YFUW
Flil nor o jaqavuxeza “eimv” ti fcex que xwoh giqdeft ak tli cufkuf urlumkc dki VEKP wiluehs bodk xe ke ob DLUB piz kai peqf zecv-ecbomoz jaja irpbuer. Ferz-uwjayiz sego voafl kayo pwe vaapd qzmivt ticx oq u OLD tuhaika zgiv’s pux a cur lovd caksv ec.
➤ Gqadxa Zomy-ujnegid ju PXEL-oycotuy, krut pyujv Wukn Basuokx.
JBEW-uyfatij mezaxuzapg: Mowtocpe: 941 Htaedam
Khol zozzil! Kzekq maaq LucBed ajgiett le zia jpeqa poevcv eg a qal vawaveyepr tozel ici-fuvs-garu:
Client apps send HTTP requests to servers, which send back responses.
An HTTP response contains a status code and some content. Text content is usually in JSON format and may contain URIs the client app can use to access media resources.
HTTP requests follow the rules of the server’s REST API, whose documentation specifies resource endpoints, HTTP methods and headers, and how to construct POST and PUT request bodies.
You can send simple GET requests in a browser app. Use cURL or an app like RESTed to create and send requests and inspect responses.
The documentation for api.raywenderlich.com includes a sidebar where you can create an HTTP request then generate Swift code for your app or send the request to a Mock Server or Debugging Proxy server.
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.