簡體   English   中英

無法在r中使用httr包復制Python request.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>

非常感謝任何有關如何進行的指導。

希望有人可以詳細說明以下各項為何起作用,和/或如何區分“錯誤”版本中發送的內容與該“正確”版本中發送的內容:

response <- POST(url,  body = list(pnodeList = list("5021072"), 
                                   startDate = "2014-04-01",
                                   endDate = "2014-04-01"),
                 encode = "json")
)

這本《快速入門指南》促使我嘗試以上操作。


添加verbose()允許您查看發送的內容:

POST(url,  body = list(pnodeList = list("5021072"), 
                       startDate = "2014-04-01",
                       endDate = "2014-04-01"),
     encode = "json",
     verbose()
)

最初嘗試時最明顯的缺陷是在python版本中, pnodeListlist

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM