使用R和x-www-form-urlencoded的POST请求无法编码Payload

时间:2018-04-24 13:54:10

标签: r api post

我试图在POST请求后从API获得一些响应。

从REST我可以轻松访问并获取所需的数据。 从R我在将有效负载发送到服务器时遇到问题。

这是R:

中的代码
query <- list(token = mytoken,
              module = "TeleMesura",
              class = "TeleMesuras",
              action = action,
              data = mydata)

  response <- POST(url = myurl,
                 body = query,
                 encode = "form", 
                 verbose(),
                 add_headers("Content-Type" = "application/x-www-form- 
  urlencoded"))

问题出在"mydata"以及如何将R编码发送到服务器。 REST中引入的正文的原始数据参数是:

{"cups" : "ES0XXXXXXXXXXX", "initDate" : "2018-01-03" , "endDate" : "2018-01-03" }

获取此字符串&#34;字符串&#34;在R我必须强迫它:

data_hard <- paste("{",dQuote("cups"), " : ",dQuote(CUP),", ",
                  dQuote("initDate")," : ",dQuote(as.character(date_start))," , ",
                  dQuote("endDate")," : ",dQuote(as.character(date_end))," }", sep = "")

哪个收益率:

"{“cups” : “ES0XXXXXXXXXXD”, “initDate” : “2018-01-03” , “endDate” : “2018-01-03” }"

REST将其编码为:

*%7B%22cups%22+%3A+%22ES0XXXXXXXXXXX%22%2C+%22initDate%22+%3A+%222018-01-03%22+%2C+%22endDate%22+%3A+%222018-01-03%22+%7D*

当R发送到服务器时(由于verbose()获得)

*%7B%E2%80%9Ccups%E2%80%9D%20%3A%20%E2%80%9CESXXXXXXXXXXD%E2%80%9D%2C%20%E2%80%9CinitDate%E2%80%9D%20%3A%20%E2%80%9C2018-01-03%E2%80%9D%20%2C%20%E2%80%9CendDate%E2%80%9D%20%3A%20%E2%80%9C2018-01-03%E2%80%9D%20%7D*

我最后在REST客户端附加了两个截图。

请帮我在正文中正确引入这个数据参数?我尝试过使用jsonlite和serializeJSON,但没有成功。试图通过application / json访问从REST失败,所以我想避免这个选项。谢谢!

enter image description here enter image description here

0 个答案:

没有答案