-1

Im trying to get JSON from webapi to R

From the following website http://wbes.srldc.in/Report/GetCurrentDayFullScheduleMaxRev?regionid=4&ScheduleDate=31-05-2020

When i try in browser, getting a proper response.

However when i try in R using any method, the fetched data is of a different html page without any JSON format.

Im just starting with R without any programming background. Please help

Ansha
  • 1
  • This is the error im getting. Error in parse_con(txt, bigint_as_char) : lexical error: invalid char in json text. – Ansha May 31 '20 at 19:30
  • 1
    Thanks for including the error, please [edit] your question and place that error text *plus the code you used that returned the error* (in a [code block](https://stackoverflow.com/editing-help), please). We can typically help better when we have something clear to start from. Thanks! – r2evans May 31 '20 at 20:11
  • Further: that web page appears to ask for a login, so I don't know that many people are going to be able to assist without clear mid-steps on your part. This will help significantly if you can walk through what you've tried and what you're seeing. My guess is that if you are not yet, you will need to use `rvest` in order to do session management. (Perhaps realize that logging into a browser and then transferring a URL to the R console typically does not bring your authentication with the URL. Apologies if this is known, but it's not always intuitive.) – r2evans May 31 '20 at 21:22

1 Answers1

2

I feel your pain, it seems the website you're trying to access checks for user-agents to avoid scraping. I'd set a common user agent in httr (Chrome or Firefox are fine) and perform a single GET request. This should get you through scraping the MaxRevision value:

library(httr)

url <- "http://wbes.srldc.in/Report/GetCurrentDayFullScheduleMaxRev?regionid=4&ScheduleDate=31-05-2020"
ua <- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"

res <- GET(url, user_agent(ua))
content(res)
#> $MaxRevision
#> [1] 216

Created on 2020-06-01 by the reprex package (v0.3.0)

anddt
  • 1,589
  • 1
  • 9
  • 26