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')
"?
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
]
}'