[R] httr package syntax (PUT)
Hadley Wickham
h.wickham at gmail.com
Thu Jun 2 13:32:12 CEST 2016
On Wed, Jun 1, 2016 at 7:16 PM, Jared Rodecker <jared.rodecker at gmail.com> wrote:
> Greetings fellow R users.
>
> I'm struggling with the syntax of submitting a PUT request
>
> I'm trying to insert a few PUT requests into some legacy R code that I have
> that performs daily ETL on a small database. These requests will add users
> to an email mailing list in MailChimp.
>
>
> I have been able to get my GET requests formatted into syntax that R
> (specifically the httr package) accepts:
>
> GET("
> https://us10.api.mailchimp.com/3.0/lists/list_id_XXXXX/members/MEMBER_HASH_#######",
> query = list(apikey = 'XXXXXXXXXXXXXX'))
>
>
> However when I try to do something similar for PUT requests this simple
> syntax isn't working - you can't just pass the API KEY and/or requested
> parameters directly through the URL. I get a 401 error if I use the same
> syntax I used for GET.
>
>
> I believe that I need to use the CONFIG option to pass the API key (either
> using AUTHENTICATE or ADD_HEADERS) and the requested parameters in the BODY
> to get the PUT request to work but I can't get the syntax to work - this
> gives a 400 error:
>
>
> auth <- authenticate("anystring", "XXXXXXXXXXXXXX", type = "basic")
>
> parms <- '[{"email_address" : "some_user at domain.com", "status_if_new" :
> "subscribed"}]'
>
> PUT("
> https://us10.api.mailchimp.com/3.0/lists/list_id_XXXXX/members/MEMBER_HASH_#######
> ",config=auth,body=parms,encode="json")
>
>
> If anyone can point me to a more flushed out example that would be
> amazing...but even just some tips on how to get more info on my error
> message to help me troubleshoot my syntax would also be a big help. I've
> also been trying to get httpput (from the RCurl package) but also
> struggling with the syntax there.
If you use verbose() you should be able to see what the problem is -
httr does the json encoding for you. You want:
params <- list(email_address = "some_user at domain.com", status_if_new =
"subscribed")
PUT("https://us10.api.mailchimp.com/3.0/lists/list_id_XXXXX/members/MEMBER_HASH_#######",
config = auth,
body = params,
encode = "json"
)
Hadley
--
http://hadley.nz
More information about the R-help
mailing list