简体   繁体   中英

Trouble replicating a Python requests.post with the httr package in r

This works in Python using the requests library:

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)

I'd like to replicate the same in R using the httr package. This is what I tried without success:

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')
                )

here is the response I get from the R code:

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>

Any guidance on how to proceed is much appreciated.

Hopefully, someone can elaborate on why the following appears to work and/or how to dissect what was being sent in the "wrong" version versus what is being sent in this "correct" version:

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

This Quickstart Guide is what led me to trying above.


Adding verbose() allows you to see what was sent:

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

The most obvious flaw in your initial attempt was that in the python version, pnodeList was a list .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM