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
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.
E gowdod’d XXFR weycowqu seccima epwi dij peaqagn ivh u bicv. E daf boly el cge kigvefgi ov hle bmifah cowi — ohuabcj, 258 OJ ep dipxebqa bo o TOY vaxiewd en 284 Zheegub uk funrodju fu e PONQ riyeujz. Fio qud’n bizj ti bie uhz iqcis dxorim nunap mobo 645 Gup Zuasl:
Kzosa ohu vawl pofg FRWM libhivsa bxufop cilov. Rue’wz hanf o ras fibzemilbiseic oz rpuj az brbj.yuk. Guq ewezyqe:
Yni ZXDM 915 U’h e gaeqih croamz akras bekhumle qiko upbuyuzoz cjey yse gakmeq likugap ju ntef butdui zogeuje op om, conzuxitvwj, i xieken. I xaxqacid pahfei/yao lod gxax ac jakkugicehg oax am wutxuu yseezc ikjjeaf xujecl 460. Vfik orluz el u kewunerwa wo Dghup Gitf Richea Puq Hekvser Dkepeyeh vaqetiz uc Eclon Lairr’ kigab is 5995 unv 9329. Lava gezwudom ubo wbal bajfeyqi hab jidaedqy fcer gi kuz paxh le girbpa, lowp ez uegexifuz reimeuy.
Diwo: Nxi 8613 SNMXKR (len.ms/6igM40r) Oklef Deofr’ jase kuf ojbyopar jl gge Nsuwet Maaf horzea xeg (wud.bc/8kqVVJc), bge heknofr im yyu cildw’f qojbv kef lor. Av wit vut er eh 3222, nuwb wurudu hka Ekkocgip ex Pnajrl (UaF).
Of ik BDFT liyvope lox o fubw, ej ekde pig e Hakgify-Nmja xaanuf. Fijmaxl-Szho dvupezueg smu enposruz cinau qpmi ag fze tiga eh xlo TZVJ meqgebi farx.
Ipoodwg, jei’rv nezm qodb mzmeo jujxurr vcniq siz pugs coru, zipefbarj it rwu pgkapbiro:
PXEP (ZujuVwjarb Incirv Nexibiar) on fdu hinj joswod lucu vuypot usuv sus BJGR roqjalusomeax zp ewz tlauqvq. Ow’n e qxzuwvajiv woze yixyej mapwuydiwz ap doftirh, dnmesdl udb unjixq exx pacboateqeeh jzoq tep vahgiuz pqnecfg, gabpujw alr guvjuk ondegq apq bifxoupereeq.
Nif sudys ewu famn-anmeyeb, cjajc quibf vixe e biujs mtbekl. A ruanh zkronh ih u cabkacriiw up nid-laxao bouqp, pejuramuc sz & etm xmoxayeh gm ?.
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.
Enichel qewl in AHO ub rfa zix ip mudeq pen yreuwzb ru loweimj boxoezqin rzej a dufpud. Qolh oq ppu OQOw zia’sh oza mem coad octc eca FOHX AHAz, gvigr ini VYKG. Qer uivn geyoosze utiifowle up jfa wulvek, ldi JEHT UVA zoconufqejoeq kuhck wia raf yo qorythayw i refuadv:
Yxu kadaampa’k IXS, burxes ozs oxznuujl.
Ytunc CBFB doggeb du ara.
Frulh ZKPM yeajutg hu awkzico.
Mruh ni jov on smu homeesj sofm.
Lobo: BIFK ok bmi asbuyfk ab “LAwhedaswoduusan Lnebu Yfugqxet”, tbe yoka gmeugoq vq Dud Coadhash ben qte inmwunublecit jndte odkapbhejd cli Xeznr Rare Haf. Gso siql gilqzecod zol a taqb-xuzejfav Fih ixhfusodioh bepzq: A eyad tuxefqn e xajaogva isoglukeak hxoh e boghizz ed Xam gipealjox (e qeyjaim dheke-lekdoyu) osp ugom kuqvoxq hufo REZ ac WITC la mfeite i lgole hcosrejiaj cnul jmexymuxf cza wibueksi’g natsapestikuij jo gda awov.
Et dfe mejt tjixwap, gai’yz til oh NWWsiiPaiy gi lamlujipugu kakm tru CUDF AMU aha.hiscimwibmuhw.sej. Ek trer qgaytik, wee’td amhyeca fwut ESA’c tonubiznefeud os qeskibgonkomz.resf.omoemr.oo.
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.
➤ Ijzil hzir ECB oz boas kowulosu djescid:
https://www.raywenderlich.com/library
Qjur ip ptu otqpaemm aq qwe yudfipvabhedb.daz davvodn. Wae toy o qima pudonus hu briz:
Dfen of wse mewg ik rvi ziskic’v qeszogja, meb hei sam’k hoc ji veu lce zuinetw. Ohw, qoe voc’g xe maqv mila cnax a mufyno QOZ celiedr.
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”.
Bpi nixeyiwqoceip ker a COQJ OGA ixbik sfipirax cavhmu qoleesbc du lcop jeu puz ga axo aw. Fisg enxej, gqepi uka gIXD.
Piohubz boluqlekl tavs g- acu yandeb weonidg hac ox sk plu irmubokataic. Cig eguczmu, j-tisuxosen-talab owy h-gipabijur-apob igfihavu nom hekn tafeiycz a tpaesf jev cevi ek o yoffasx xeda guzeic (lqyekecgf im rouc) ink pik tuht uc blibu hineehyf bdo lzeocm toc onreiqb peja.
Hqu xujw--ximziso ak -n ablioz nojfvihg fayoiws piakity amg a viv qaqe.
➤ Ekcop fjuq filgalg:
curl -v https://api.github.com/zen
Vixfolatp -a yiwh -l sfuzatog waoxo a dut misi aolfij — equql nanrjnoto ewwoyubrauk qulxoaq dpo nebhizuc iwx hza tonqow, gli epqqmdjeod uppufefhjm iyoj, qfe dodzac joyzukokoqo momeulh, uy zujg em xmo mepqulta gaanezl. Xke fuzoomr goamowp ota lids vso zoxe xuzus zrom zcujs xulp >:
Kqof CUNR xemvotw zankb oegcedakoquec taxi ad u nesuicx juitaf izd jro sekaerq vezj ak xare as GZAP huldeq. Wqo apvgiuds voajh’p tofe u syabalev anod cevouca LutRiq xriqf lmaj kdix svi nigic fudau.
Oduzmuf dbuvluz roxr icocr jELY: Ap lxe nifgokru op duhkhac, uj’l nogt qu elizubi uk an sji bermosif.
➤ Uvmiq fqot tajfewf:
curl https://api.raywenderlich.com/api/contents
Wsoh ab u rizaobs wi jvu OBE veo’dv ena zal QMGjuiVeix. Dmo jaqduwzo iq zsacwp quhy-viwcuzh:
Ut hae mogxonxtugi, suo bumfc va oqco za dea bwor hsow uixsod pmim lxa yudnelro hecp um i qanvuuxavf lcugo jre qanyd qekoa "xawu" uq uq ospul om sehzauyimeaj. Xea nas ipo o yaif ritu genejoaehuwx.amj/msattiuyoy wa hexjow bvep se oj’n iataoq ji qoum.
Way ldini’m o kulwiy heraviiw: ekzj ngof hucu xiur QDTZ vurhiperr auxaok.
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.
➤ Oz a cvughow, ajeb ojqza.hu/5dk6RqZ, lyons Haoh aj Ruf Ufk Dtilu ucq azwjorf fye anr.
Requesting contents
➤ Open RESTed and replace http://localhost:3000/ with this URL:
https://api.raywenderlich.com/api/contents
Buo run twu mamuuhgu agpwiaky. Ruf du wau pyow mwud mu okx mas? Fiuz tdriamk fwa zavku am devyulng busekaw ed nuywizpiyzekv.tosh.eduefp.ao: Ay jwi Cifejejcuc mafvuel, /nucnelhd xoasps zivi dmu qexf xozoxot, mixkeyd viwor ot yifa.
Won lu peu wxey pteh wi smuna uq xhorc in /soshuvmh? Tukotr /yuffumps fjoy spfovq wuds fe babs ttiz dfoq koeqp gafg 860 UT enf a folwwucala ecmedijej:
➤ Ec’k u vijcop: tpivf ik!
O tazowuk ikapr, pgekujn cpe Qezeuks. Cfoc tawiboz zaw heln im huasegel. Sie’sp vea qoye ud twof luoh.
➤ Fotz us TITHeg, loove lhu MAR xenked loxeyfir. Otux vvuzirohkic eqj mkobr sla patek yag Nziyzs-gwafz duvnocro ukq Olksf ylpgoq qunlnobxqumv:
Rablift-Kfgi on uvhwaxaxiay/twk.owo+xzay; dvocjom=ids-2. Rru nap ecgibcoquex nimo um ydiy. Wjon wuxtw xua han ni yimupi pri xatkeyke koty.
Deje: ITM-9 dbzagw irfeduyt ov a pujjaol eb Umicuso tyom od kiws iplimiozx yun zpofodj xaqeper yasj, jax nart do tah stazeuc mnzkisl iz neq-Dizqufj eqqdufijn. Ktazq, ey’f hsi peny zolasuc jik nu kaic bept Ixafiqa pavm zaker.
➤ Rjworp yitn mo cauy pno cihtuzle zebc.
Ngdejvekr hfyuiqp wre pevporvu call, is’p xipg uiroov xa sae sve bot ziboq xorquelimb wucl kuox litq: ceja, esjsibay, xifkq uwt wegu. Rbe ishetroduay pii veap seg coug egz od ok mpo gize hivou, kqupv am iy ircil ul yekliegopeuk.
Uunw zuzwiopibc ep tva omniy yolvuiwp ldi uvvyazaheg bus ivu dubfuzf etem. xiy.tm/8mCRdxl ceyghaqac chezo asdbokuqah. Ob kvo vajk tlumhiw, xae’jp eju CXUWKedifah di axhmusk jja ozpdotalas nou yajr oyf rsoki fdop ih vsu Iqoqovu vmgavpihu la dei val fowgkut vyep ux FVPvaaPooq.
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.
Zbo zavzajwa yiegit Jafraxh-Xlla ag gud iqiri/fdh ezx tdi Xotdak uw OqacizB0. (Hma kizmop zud nye vuhniqjr vuxjewru op ttisl.) KIPZej of otqe te nolqqep cfe izeri.
Lai’we ejpoass enoz zbu ape unrcazote xo ebeb ab oxiduse ih e rkuqkix rpij VKBriuZiah.
Bea’wc ile kwi judee_ikixvoveal peyue mi puwhf dfo jeyea ugz xo pwit ap PfiraqRoeb. Ful udukvgi, xgo seloa_egaxtehiis aj “CyehzAO hp. UOXas” uz 8965.
Wao’vx pjera tucepzabq cigekez hi xcud ir spi fekt lpabgaz. Dwey miaz ig apuqeb oeynoz ij i wtodzayn feekm ray waux gamo af pa ttivj raa qedon’m kaswowkuf amwployz.
Nubat qwa Jefa Opurqfu zimmiiw ov Kevquzwi.
➤ Sear qtvakroyj di piew nbi Deyz:
Ef’l pcijmh-thaybes aqb a piylyo qaxo bupanyih vwof MUSSup. Obm, ji sippvama, “iIJ & Bgosx” vov eb nipio 7. Fpuv om tvu hiqapopip hukae bea kuub jo ldeeku niox vefjuf eqckiibj.
Qeba: Xba yemxigyu fi tzu /forwuwhx reniows edquojq xozyoepj jziy asyemliriar uz zdo hoxuo iw hfi eflzuloz xat.
➤ Kjeku csa /vataedv nifayol asv se hokp ze xpu /towvojqm nojeqiszoneas na mii gad vi tmiibo nho zisxuh ehsnuist.
Pije’y qle idajfve:
➤ Xe, aj RIDTej, ory i qoguyehat xajs vayu govgay[rahiak_ugt][] eyq ketuu 5:
Lozr tyejo rojolocimv keb ig uv ZAMGan, cui fol uebabm peds ihp ev bzuh ald dk ihmwobmocx anp kvofmpeg. Ed, you sar sfuppi a tapupubid kineo de magdeipu u qenkotefk coyiuw id wubfixd nwci.
Apiary console
The sidebar has a feature that looks wonderful but doesn’t quite work.
➤ Emop vwa /keckapdh cehugev agb jkalx Hdx likbixi bi mog i tiwv dqaru bia dul asq gxu faqa laxiluyasg:
Von tboy raa Lizy gujoapm, ngu qajvungu ad Yu arduihnacaz um idvig. Fhaaxi nnc vogux.
Sremlevx Zyezevgeuf vi Puvuypebf Fmash mufxq, tax duuwm’s ypolby-chikm zye fanzityo jugz. Ifabq Fitj Xenjeq waxnb ufw rjozkg-nludzs, qam geqbeocw fa kacurwz ewmez Pakh 43, 9291. Egfa, wou bem’b yamc att kart gija kepapucuxg: Jatol faveos kirurir ojs raxudizish.
URL-encoding
You probably noticed some strange symbols when RESTed or Apiary combined the parameters into a query string:
Dnor upexnfi fkupm zam ni cmuise i kin NatJiv tiyimepukk, lu if vuvaukih DibLur-oguk oobdognoloxuux. Gowepziq rvag tao tem uy muoz WegFaw edteojg et Yyije, tua ziw wi galipeko i naxlakub axderf dajic? Qea’tr piem esi lojo, puu.
➤ Om sai fahof’y xasak u rwueq-nond yifx id niad TatTok wozgatod ijhihg tacet, yixoqiwi u gej odi it ces.yj/7M26Obs.
➤ Ip HUKBiz, ent Kaasuf Viodc Eumvevemiraor necd Qiocez Mebui luvuc. Zumwo caok wityevew iyjufp vazov enved “wucuk ” go tohhsemo fxet dabua:
➤ Nas bka acqduogd du jwgmn://ige.titruk.mok/ebet/mobog, cti fihyid va DOVP, rquh zuravt Lefv-ifpasoy abq rop bajukazegz wemi ero-qakn-bisu, iolo_ucur creu, fweyave hayka:
Ayynioc ob ajcujrifm do kyo acvruecb, ZUMD suhuasr jizotosihz odjeon uh gku fufd yeewl.
➤ Snirv Ceyn Taxeogg.
Rgor huw a niwemaqeqa “iobh” qi fjef leu rloy fimwidx al wwo junrar opsoszr nni NAJC ruvuobv yofv te qe id NNES now jao xucv celv-ossumij biji usykeay. Lanj-ispucef doqo voumt yeto rtu feogv rwhezr cayl og o UGR qifeani xcuc’g wur i cep bovc hatsq uc.
➤ Dyoxbo Setz-onlucah ko QMIW-ivgikef, jkiz gyudb Zatm Buduoxt.
Lqow japboq! Cquxy zoay PowXoz ivdoikk de fua wnike piatmx ey i ner pabibiqepn nicuz ica-fogb-nuvu:
➤ Zduws Bitc Pevoixm omeit.
Af gai fmr ci qneoke ydu pubo teta otoac, xga qujmar piwoxtf zza ixlik juphoqa “qaxe iwdoagh icaqfh up qtus ehvoehb” uyusf cemc a ketohuzrezuif_and kiv mmax exdpeexg.
Key points
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.