0

I have this Curl code below which runs fine in https://reqbin.com/curl:

curl -X POST https://historicdata.website.com/api/DownloadListOfFiles
-H 'content-type: application/json' 
-H 'ssoid: INPUT_SESSION_TOKEN_HERE' 
-d '{
    "music":"Rock",
    "plan":"Basic Plan",
    "fromDay" : 1,
    "fromMonth": 3,
    "fromYear" : 2020,
    "toDay": 3,
    "toMonth" : 3,
    "toYear": 2020,
    "eventId": null,
    "eventName": null,
    "marketTypesCollection": [ "GUITAR" ],
    "countriesCollection" : [ "GB", "IE" ],
    "fileTypeCollection" : [ "M" ]
}'

The response is a list of filesnames which is correct.

I would like to run this code in R and I have tried to convert it with chatGPT although it gives unexpected output

This is into R code converted code:

library(httr)
library(jsonlite)

session_token <- "INPUT_SESSION_TOKEN_HERE"


# Define the URL and headers
url <- "https://historicdata.website.com/api/DownloadListOfFiles"
headers <- c(
  "content-type" = "application/json",
  "ssoid" = session_token)


# Define the request body
body <- list(
  music = "Rock",
  plan = "Basic Plan",
  fromDay = 1,
  fromMonth = 10,
  fromYear = 2023,
  toDay = 2,
  toMonth = 10,
  toYear = 2023,
  eventId = NULL,
  eventName = NULL,
  marketTypesCollection = "GUITAR",
  fileTypeCollection = "M"
)

# Perform the POST request
response <- httr::POST(url, httr::add_headers(.headers=headers), body = jsonlite::toJSON(body))

# Check response
if (httr::http_error(response)) {
  stop("HTTP error: ", httr::http_status(response)$reason)
} else {
  cat("Request successful.")
}

# Extract and print the content of the response
content <- httr::content(response, as = "text")
cat(content)

This is the response:

Request successful.> 
> # Extract and print the content of the response
> content <- httr::content(response, as = "text")
> cat(content)



<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ngErrorRedirect</title>
</head>
<body>
<div>
Error
</div>
<script defer src="https://static.cloud.com/beacon.min.js/v8c1696crossorigin="anonymous"></script>
</body>
</html>
> 

Can someone identify why the R code does not provide a list of filesnames as the curl code does?

2
  • It's impossible to test without a valid session token. But look at the output of jsonlite::toJSON(body). That doesn't really match exactly what you are passing with curl. jsonlite::toJSON(body, auto_unbox = TRUE) might get you closer. And countriesCollection seems to be missing from your R code. Commented Mar 1, 2024 at 14:57
  • @ MrFlick That is fantastic! Adding auto_unbox = TRUE is the solution. Thanks a lot!! Commented Mar 1, 2024 at 16:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.