使用 r 中的 httr 包复制 Python requests.post 时遇到问题
Trouble replicating a Python requests.post with the httr package in r
这在 Python 中使用请求库有效:
plist5 = ["5021072"]
data = {"startDate": "2014-04-01", "endDate": "2014-04-01", "pnodeList": plist5}
data = json.dumps(data)
url = 'https://dataminer.pjm.com/dataminer/rest/public/api/markets/realtime/lmp/daily'
headers = {'Content-Type': 'application/json'}
response = requests.post(url, data=data, headers=headers)
我想使用 httr 包在 R 中复制相同的内容。这是我尝试过但没有成功的方法:
url <- 'https://dataminer.pjm.com/dataminer/rest/public/api/markets/realtime/lmp/daily'
response <- POST(url, body = list(pnodeList = "5021072",
startDate = "2014-04-01",
endDate = "2014-04-01"),
headers = list('Content-Type' = 'application/json')
)
这是我从 R 代码得到的响应:
Response [https://dataminer.pjm.com/dataminerui/]
Date: 2016-02-24 15:28
Status: 200
Content-Type: text/html
Size: 159 B
<!-- Plain HTML page that kicks us into the app -->
<html>
<head>
<meta http-equiv="Refresh" content="0; URL=pages/public/index.html">
</head>
</html>
> content(response)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<!-- Plain HTML page that kicks us into the app --><html><head><meta http-equiv="Refresh" content="0; URL=pages/public/index.html"></head></html>
非常感谢任何关于如何进行的指导。
希望有人可以详细说明为什么以下内容似乎有效 and/or 如何剖析 "wrong" 版本中发送的内容与此 "correct" 版本中发送的内容:
response <- POST(url, body = list(pnodeList = list("5021072"),
startDate = "2014-04-01",
endDate = "2014-04-01"),
encode = "json")
)
This Quickstart Guide 是促使我在上面尝试的原因。
添加 verbose()
可让您查看发送的内容:
POST(url, body = list(pnodeList = list("5021072"),
startDate = "2014-04-01",
endDate = "2014-04-01"),
encode = "json",
verbose()
)
您最初尝试中最明显的缺陷是,在 python 版本中,pnodeList
是 list
。
这在 Python 中使用请求库有效:
plist5 = ["5021072"]
data = {"startDate": "2014-04-01", "endDate": "2014-04-01", "pnodeList": plist5}
data = json.dumps(data)
url = 'https://dataminer.pjm.com/dataminer/rest/public/api/markets/realtime/lmp/daily'
headers = {'Content-Type': 'application/json'}
response = requests.post(url, data=data, headers=headers)
我想使用 httr 包在 R 中复制相同的内容。这是我尝试过但没有成功的方法:
url <- 'https://dataminer.pjm.com/dataminer/rest/public/api/markets/realtime/lmp/daily'
response <- POST(url, body = list(pnodeList = "5021072",
startDate = "2014-04-01",
endDate = "2014-04-01"),
headers = list('Content-Type' = 'application/json')
)
这是我从 R 代码得到的响应:
Response [https://dataminer.pjm.com/dataminerui/]
Date: 2016-02-24 15:28
Status: 200
Content-Type: text/html
Size: 159 B
<!-- Plain HTML page that kicks us into the app -->
<html>
<head>
<meta http-equiv="Refresh" content="0; URL=pages/public/index.html">
</head>
</html>
> content(response)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<!-- Plain HTML page that kicks us into the app --><html><head><meta http-equiv="Refresh" content="0; URL=pages/public/index.html"></head></html>
非常感谢任何关于如何进行的指导。
希望有人可以详细说明为什么以下内容似乎有效 and/or 如何剖析 "wrong" 版本中发送的内容与此 "correct" 版本中发送的内容:
response <- POST(url, body = list(pnodeList = list("5021072"),
startDate = "2014-04-01",
endDate = "2014-04-01"),
encode = "json")
)
This Quickstart Guide 是促使我在上面尝试的原因。
添加 verbose()
可让您查看发送的内容:
POST(url, body = list(pnodeList = list("5021072"),
startDate = "2014-04-01",
endDate = "2014-04-01"),
encode = "json",
verbose()
)
您最初尝试中最明显的缺陷是,在 python 版本中,pnodeList
是 list
。