0

I am trying to get data from a website (using API)using R. I got data using httr/POST and did some analysis. Recently, I found R was not giving me any data for some specific parameters but for the same parameters using terminal curl and Python/POST, I had some data (which are same). I am wondering what am I missing in case of R. Please see below what I am using (where myurl is the secret api address). Thanks in advance for your help.

In terminal:

This is defined in API manual and I wrote it accordingly.

curl myurl -d "timeMin= 965192400000" -d "timeMax=1533186000000"  -d studentId=117

Same result if I used

curl -X POST myurl  -d timeMin=965192400000 -d timeMax=1533186000000 -d studentId=117

or

curl -X POST myurl  -d timeMin=965192400000 -d timeMax=1533186000000 -d studentId="117"

In R:

res <- POST(myurl, body = list(timeMin = 965192400000, timeMax = 1533186000000, studentId = "117" ),encode = c("form"))

Then I used res$content, rawToChar and fromJSON

In Python:

res = requests.post(myurl, data=[
  ('timeMin', 965192400000),
  ('timeMax',1533186000000 ),
  ('studentId', 117)])

Then I used json.loads on response.text and made it python dataframe.

Maik
  • 170
  • 7
Neon
  • 1
  • Try posting to a service like https://httpreq.com/ where you can compare what's actually being sent to the server to see how the POST messages differ. It's possible that the server is looking for a certain user-agent or something else that's not being explicitly set by your invocations. Hard to say without a clear [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Sep 10 '18 at 15:19
  • Maybe you just need `res <- POST(myurl, body = list(timeMin = "965192400000", timeMax = "1533186000000", studentId = "117" ),encode = c("form"))` because those numbers are too big to be "numbers" in R. – MrFlick Sep 10 '18 at 15:21
  • Yes, I know it is difficult to explain without knowing the details. Sorry for that. Would you please explain a little bit about why you think these numbers are too big for R. But your trick (changed as timeMin = "965192400000", timeMax = "1533186000000") works and now I get the same results as terminal and Python. Thanks a lot. – Neon Sep 10 '18 at 18:24
  • 1533186000000 is too larger a number to be stored in an integer. It needs to be stored as a floating point number. Doing so loses precision so R can't get back to the exact same number. When using quotes, you are string the value as a character so the limit of the range of numbers that can be stored as an integer doesn't apply. – MrFlick Sep 10 '18 at 18:32

0 Answers0