how to convert this Curl code into R code?

37 Views Asked by At

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?

0

There are 0 best solutions below