1

I try to send a request to API, use RCurl library.

My code:

start = "2018-07-30"
end = "2018-08-15"

api_request <- paste("https://api-metrika.yandex.ru/stat/v1/data.csv?id=34904255&date1=",
                     start,
                     "&date2=",
                     end,
                     "&dimensions=ym:s:searchEngine&metrics=ym:s:visits&dimensions=ym:s:<attribution>SearchPhrase&filters=ym:s:<attribution>SearchPhrase!~'some|phrases|here'&limit=100000&oauth_token=OAuth_token_here", sep="")
s <- getURL(api_request)

And every time I try to do it I have the response "Error 400" or "Bad Request" if I use getUrlContent instead. When I just open this url in my browser - it works correctly. I still couldn't find any solution for this problem, so if somebody knows something about it - please help me, kind man =)

1 Answers1

0

There are several approaches you can use in case the URL is right. First you can add the following parameter to the getURL function. Setting the parameter followlocation equal TRUE allows to follow any "Location:" header that the server returns as part of the HTTP headers. Processing is recursive, PHP will follow any "Location:" header.

> s <- getURL(url1, .opts=curlOptions(followlocation = TRUE))

If this is not working an alternative way is to use the XML package by calling the htmlParse method instead of getURL

> library(XML)
> s <- htmlParse(api_request)

Another approach would be to use the httr package and call the GET function:

> library(httr)
> s <- GET(api_request)
Daniel
  • 552
  • 2
  • 9
  • 29