POST R 中的请求与 Python
POST requests in R vs. Python
我 运行 POST 在 Python 中请求并且能够使用以下代码解码文本数据:
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')
上面的代码工作正常,我可以看到结果,我最终可以将所需的信息转换成数据框。
我需要的是将上面的代码转换成R
。我在 R 中编写了以下代码:
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)
在“R”代码的情况下,我也收到了“成功”通知,但我仍然停留在“如何”提取数据集,就像在 Python 代码“response.text.encode('utf8')
”中一样?
因此,问题出在 params
定义中
正确的语法应该是(注意我不需要 $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
]
}'
我 运行 POST 在 Python 中请求并且能够使用以下代码解码文本数据:
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')
上面的代码工作正常,我可以看到结果,我最终可以将所需的信息转换成数据框。
我需要的是将上面的代码转换成R
。我在 R 中编写了以下代码:
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)
在“R”代码的情况下,我也收到了“成功”通知,但我仍然停留在“如何”提取数据集,就像在 Python 代码“response.text.encode('utf8')
”中一样?
因此,问题出在 params
定义中
正确的语法应该是(注意我不需要 $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
]
}'