-1

I ran a POST request in Python and able to decode the text data by using below code:

import requests
import pandas as pd
import json
import numpy as np

url = "SomeURL"

payload = "{\r\n    \"tagCsv\": \"LU_HIST_SVR.CPR01_BPTransSlope,LU_HIST_SVR.CPLEM_PTransSlope,LU_HIST_SVR.C875_5_DPTTransSlope,LU_HIST_SVR.C919_6_DPTTransSlope\",\r\n    \"interval\": 2.88,\r\n    \"frequency\": \"mi\",\r\n    \"samplingMethod\": \"last\",\r\n    \"quality\": \"GOOD*\",\r\n \r\n    \"startDate\": [\r\n        2020,\r\n        6,\r\n        1,\r\n        1,\r\n        21,\r\n        20\r\n    ],\r\n    \"endDate\": [\r\n        2020,\r\n        6,\r\n        7,\r\n        23,\r\n        21,\r\n        20\r\n    ]\r\n}\r\n"
headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer token code...'
}

response = requests.request("POST", url, headers=headers, data = payload)
test_string = response.text.encode('utf8')

The above code works fine, I can see the results and I can eventually convert the required info into a dataframe.

What I need is the above code converted to R. I wrote the below code in R as:

 library(httr)
library(jsonlite)
library(data.table)

url <- "SomeURL"
params <- list()
params$variables <-  '[{
  "tagCsv": "LU_HIST_SVR.CPR01_BPTransSlope,LU_HIST_SVR.CPLEM_PTransSlope,LU_HIST_SVR.C875_5_DPTTransSlope,LU_HIST_SVR.C919_6_DPTTransSlope",
  "interval": 2.88,
  "frequency": "mi",
  "samplingMethod": "last",
  "quality": "GOOD*",
  
  "startDate": [
    2020,
    6,
    1,
    1,
    21,
    20
    ],
  "endDate": [
    2020,
    6,
    7,
    23,
    21,
    20
    ]
}]'
  
headers = c('Content-Type'="application/json",'Authorization'= "Bearer token code")
r_POST <- httr::POST(url,body = params, add_headers(headers), encode = "json",verbose())
r_POST
http_status(r_POST)

I received the "success" notificationin the case of "R" code as well but I am stuck at "how" to extract the dataset like in Python code "response.text.encode('utf8')"?

CodeMaster
  • 431
  • 4
  • 14
  • 1
    R doesn't have dot notation like Python. It looks like your Python is saying take the response encoded as text as UTF-8. Have you tried looking at the object `r_POST` and seeing it's structure? R generally defaults to UTF-8 so you might try `r_POST$response` – Adam Sampson Aug 27 '20 at 21:00
  • 1
    Or if the data is coming in as raw you might try `fromJSON(rawToChar(r_POST$response$content))`. But we'd need to see the structure of the r_POST object to know for sure. – Adam Sampson Aug 27 '20 at 21:01
  • 1
    Try `content(r_POST, as="text", encoding="UTF-8")` – MrFlick Aug 27 '20 at 21:05

1 Answers1

0

So, the issue was in the params definition the correct syntax should be (note I don't need the $variables )

params <- list()
params <-  '{
  "tagCsv": "LU_HIST_SVR.CPR01_BPTransSlope,LU_HIST_SVR.CPLEM_PTransSlope,LU_HIST_SVR.C875_5_DPTTransSlope,LU_HIST_SVR.C919_6_DPTTransSlope",
  "interval": 2.88,
  "frequency": "mi",
  "samplingMethod": "last",
  "quality": "GOOD*",
  
  "startDate": [
    2020,
    6,
    1,
    1,
    21,
    20
    ],
  "endDate": [
    2020,
    6,
    7,
    23,
    21,
    20
    ]
}'
CodeMaster
  • 431
  • 4
  • 14