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
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

GET and POST requests in theory

W OR K IN G W ITH W E B DATA IN R

Oliver Keyes

Instructor

slide-2
SLIDE 2

WORKING WITH WEB DATA IN R

HTTP requests

Conversation between your machine and the server First: what you want to happen "methods" - dierent requests for dierent tasks

slide-3
SLIDE 3

WORKING WITH WEB DATA IN R

GET and POST

GET: "get me something" POST: "have something of mine"

slide-4
SLIDE 4

WORKING WITH WEB DATA IN R

Other types

HEAD - just like head() DELETE - "remove this thing" Many others! But GET and POST are the big ones

slide-5
SLIDE 5

WORKING WITH WEB DATA IN R

Making GET requests with httr

response <- GET(url = "https://httpbin.org/get") content(response) $args named list() $headers $headers$Accept [1] "application/json, text/xml, application/xml, */*" ...

slide-6
SLIDE 6

WORKING WITH WEB DATA IN R

Making POST requests with httr

response <- POST(url = "https://httpbin.org/post")

slide-7
SLIDE 7

Let's practice!

W OR K IN G W ITH W E B DATA IN R

slide-8
SLIDE 8

Graceful httr

W OR K IN G W ITH W E B DATA IN R

Charloe Wickham

Instructor

slide-9
SLIDE 9

WORKING WITH WEB DATA IN R

Error handling

Every response includes an HTTP status code.

slide-10
SLIDE 10

WORKING WITH WEB DATA IN R

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 { ...

slide-11
SLIDE 11

WORKING WITH WEB DATA IN R

Understanding status codes

Code starts with: 2 - great! 3 - great! 4 - your code is broken 5 - their code is broken hps://en.wikipedia.org/wiki/List_of_HTTP_status_codes Check for bad codes with http_error()

slide-12
SLIDE 12

WORKING WITH WEB DATA IN R

URL construction

Most of URL doesn't change Stitch URLs together from bits that don't change with the bits that do Saves thinking and typing

slide-13
SLIDE 13

WORKING WITH WEB DATA IN R

Directory-based URLs

Slash-separated, like directories

https://fakeurl.com/api/peaches/thursday

Use paste() , with sep = "/"

slide-14
SLIDE 14

WORKING WITH WEB DATA IN R

Parameter-based URLs

Uses URL parameters ( a=1&b=2 )

https://fakeurl.com/api.php?fruit=peaches&day=thursday

Use GET() to construct the URL with query argument

slide-15
SLIDE 15

Let's practice!

W OR K IN G W ITH W E B DATA IN R

slide-16
SLIDE 16

Respectful API Usage

W OR K IN G W ITH W E B DATA IN R

Oliver Keyes

Instructor

slide-17
SLIDE 17

WORKING WITH WEB DATA IN R

User agents

Bits of text that ID your browser (or soware) Gives the server some idea of what you're trying to do You can set one with your requests with user_agent() Add an email address so they can contact you.

slide-18
SLIDE 18

WORKING WITH WEB DATA IN R

Rate limiting

Too many requests makes for a sad server Deliberately slows down your code to keep under a desired "rate" Slows you, but avoids geing you banned from the server

slide-19
SLIDE 19

Let's practice!

W OR K IN G W ITH W E B DATA IN R