0

I am trying to do login and download the file with using a package httr and jsonlite in R language. I write the following code :

library(data.table)
library(jsonlite)
library(httr)
library(properties)
library(futile.logger)
library(crayon)
library(XML)
library(methods)
library(compare)
library(tictoc)

#-----------------------------------------------------

args = commandArgs(trailingOnly=TRUE)

host.name = "lgloz050.lss.emc.com"
port = "58443"
default.path = "/APG/lookup/"

#Set the working directory
# NOTE : this location has to be passed externally as a param 
wd.name = "C:\\Users\\sharmb5\\Documents\\R script_RR"
setwd(wd.name)

#Set Default Configurations. 
set_config(config(ssl_verifypeer = 0L))

#-----------------------------------------------------
#   Read the configurtion file
#-----------------------------------------------------
config <- fread("Configuration.csv")
config$bc <- config$testReport

# let us filter the reports which are not enabled for fetch / compare .i,e, reportMatch = NO
config <- config[reportsMatch %in% c("yes","YES","TRUE")]

#breadcrumbs & spaces handling
config$testReport <- gsub(">>","/",config$testReport)
config$testReport <- gsub(" ","%20",config$testReport)

config$URL <- paste("https://",host.name,":",port,default.path,config$testReport,"/report.csv", sep = "")

# $place_holder filler 
properties = read.csv2("resources/configuration.properties",sep = "=", blank.lines.skip = TRUE,header = FALSE,stringsAsFactors = FALSE  )
colnames(properties) <- c("key","value")

config$URL <- gsub( "\\$" , "PH_" , config$URL )
for(i in 1:nrow(properties)){
  if(startsWith(properties[i,1],"$")){
    print(properties[i,1])
    for (j in 1: nrow(config)) {
      config[j]$URL = gsub(paste("PH_",substring(trimws(properties[i,1]),2),sep = "")
             ,trimws(properties[i,2]),config[j]$URL,ignore.case = TRUE)
    }
  }

}
#result <- config
result <- config[,list(bc,TestCaseID,URL),]

#------------------------------------------------
# Fetch Report Data
#------------------------------------------------

# Auth Function
auth <- function(URL,user.name="admin", password="changeme"){
  # Only for first time login
  res <- GET(URL,add_headers("accept"="text/json"))
  res <- POST('https://lgloz050.lss.emc.com:58443/APG/j_security_check'
                ,set_cookies=res$cookies
                ,body = "j_username=******&j_password=******"
                ,add_headers("Content-Type" ="application/x-www-form-urlencoded" ))
    return(res)
}


# Fetch Function
fetch <- function(URL,save.location,cookies){
    fetch.success = TRUE
    res <- GET(URL
               ,add_headers("Authorization"="Basic *************")
               ,set_cookies=cookies)
    tryCatch({repot_data <- fread(content(res,"text"),header = TRUE);
             # write.csv(data.frame(repot_data),save.location,row.names = FALSE);
              fwrite(data.frame(repot_data),save.location,row.names = FALSE);
              flog.info(green("'\u2713' - Fetch Completed successfully ..."))
              flog.info(paste("URL : ",URL))} 
              #,warning=function(w){flog.warn(paste("warnings while fetching data "))}
              ,error = function(e){fetch.success= FALSE; flog.error(paste("\u2715 - Not able to fetch data,file not created "))})

    return(fetch.success)
    }

config$save.location = sub("TruthData","testData",config$truthReport,ignore.case = T)
response = auth(config[1]$URL)
response

but the problem with this code is when I am going to execute the code for some appliance it works well but on the second execution of same code, it redirects to the login page and downloaded file has HTML content of login page.

how can I solve this issue, such that both login and downloading will be done in the same session and it will not redirect to the login page over multiple iterations of execution page?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.