get and post req u ests in theor y
play

GET and POST req u ests in theor y W OR K IN G W ITH W E B DATA IN - PowerPoint PPT Presentation

GET and POST req u ests in theor y W OR K IN G W ITH W E B DATA IN R Oli v er Ke y es Instr u ctor HTTP req u ests Con v ersation bet w een y o u r machine and the ser v er First : w hat y o u w ant to happen " methods " - di erent


  1. GET and POST req u ests in theor y W OR K IN G W ITH W E B DATA IN R Oli v er Ke y es Instr u ctor

  2. HTTP req u ests Con v ersation bet w een y o u r machine and the ser v er First : w hat y o u w ant to happen " methods " - di � erent req u ests for di � erent tasks WORKING WITH WEB DATA IN R

  3. GET and POST GET : " get me something " POST : " ha v e something of mine " WORKING WITH WEB DATA IN R

  4. Other t y pes HEAD - j u st like head() DELETE - " remo v e this thing " Man y others ! B u t GET and POST are the big ones WORKING WITH WEB DATA IN R

  5. Making GET req u ests w ith httr response <- GET(url = "https://httpbin.org/get") content(response) $args named list() $headers $headers$Accept [1] "application/json, text/xml, application/xml, */*" ... WORKING WITH WEB DATA IN R

  6. Making POST req u ests w ith httr response <- POST(url = "https://httpbin.org/post") WORKING WITH WEB DATA IN R

  7. Let ' s practice ! W OR K IN G W ITH W E B DATA IN R

  8. Gracef u l httr W OR K IN G W ITH W E B DATA IN R Charlo � e Wickham Instr u ctor

  9. Error handling E v er y response incl u des an HTTP stat u s code . WORKING WITH WEB DATA IN R

  10. Error handling response <- GET("https://httpbin.org/get") response Response [https://httpbin.org/get] Date: 2017-08-24 20:29 Status: 200 Content-Type: application/json Size: 330 B { ... WORKING WITH WEB DATA IN R

  11. Understanding stat u s codes Code starts w ith : 2 - great ! 3 - great ! 4 - y o u r code is broken 5 - their code is broken h � ps :// en .w ikipedia . org /w iki / List _ of _ HTTP _ stat u s _ codes Check for bad codes w ith http_error() WORKING WITH WEB DATA IN R

  12. URL constr u ction Most of URL doesn ' t change Stitch URLs together from bits that don ' t change w ith the bits that do Sa v es thinking and t y ping WORKING WITH WEB DATA IN R

  13. Director y- based URLs Slash - separated , like directories https://fakeurl.com/api/peaches/thursday Use paste() , w ith sep = "/" WORKING WITH WEB DATA IN R

  14. Parameter - based URLs Uses URL parameters ( a=1&b=2 ) https://fakeurl.com/api.php?fruit=peaches&day=thursday Use GET() to constr u ct the URL w ith query arg u ment WORKING WITH WEB DATA IN R

  15. Let ' s practice ! W OR K IN G W ITH W E B DATA IN R

  16. Respectf u l API Usage W OR K IN G W ITH W E B DATA IN R Oli v er Ke y es Instr u ctor

  17. User agents Bits of te x t that ID y o u r bro w ser ( or so �w are ) Gi v es the ser v er some idea of w hat y o u' re tr y ing to do Yo u can set one w ith y o u r req u ests w ith user_agent() Add an email address so the y can contact y o u. WORKING WITH WEB DATA IN R

  18. Rate limiting Too man y req u ests makes for a sad ser v er Deliberatel y slo w s do w n y o u r code to keep u nder a desired " rate " Slo w s y o u, b u t a v oids ge � ing y o u banned from the ser v er WORKING WITH WEB DATA IN R

  19. Let ' s practice ! W OR K IN G W ITH W E B DATA IN R

Recommend


More recommend