从R查询QPX Express

时间:2014-07-21 10:59:43

标签: r httr

我正在尝试从R(httr)查询QPX Exprss(谷歌),但无论出于何种原因我得到0结果。这是我的问题:

x <- list(
    request = list(
      slice = list(c(origin = "BOS", destination = "LAX", date = "2014-07-29")), 
      passengers = c(adultCount = 1, infantInLapCount = 0, infantInSeatCount = 0,
                     childCount = 0, seniorCount = 0),
      solutions = 10,
      refundable = "false")
    )

这就是命令:

POST("https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY_KEY",
     body = toJSON(x), add_headers("Content-Type" = "application/json"), verbose(),
     add_headers(Expect = ""))

最后,Google的回复:

* About to connect() to www.googleapis.com port 443 (#0)
*   Trying 173.194.66.95... * connected
* Connected to www.googleapis.com (173.194.66.95) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: C:/Users/XXXX/Documents/R/win-library/3.1/httr/cacert.pem
  CApath: none
* SSL re-using session ID
* SSL connection using ECDHE-RSA-RC4-SHA
* Server certificate:
*    subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.googleapis.com
*    start date: 2014-07-02 13:35:47 GMT
*    expire date: 2014-09-30 00:00:00 GMT
*    subjectAltName: www.googleapis.com matched
*    issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
*    SSL certificate verify ok.
> POST /qpxExpress/v1/trips/search?key=MY_KEY HTTP/1.1
Host: www.googleapis.com
Accept: */*
Accept-Encoding: gzip
user-agent: curl/7.19.6 Rcurl/1.95.4.1 httr/0.3
Content-Type: application/json
Content-Length: 220

< HTTP/1.1 200 OK
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: Fri, 01 Jan 1990 00:00:00 GMT
< Date: Mon, 21 Jul 2014 10:39:46 GMT
< ETag: "FHaaT3rgbj6tTc1zJmPkVQ6bD-8/wa9h__cUdEwRE2bp0yW5NTA6fec"
< Content-Type: application/json; charset=UTF-8
< Content-Encoding: gzip
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< Server: GSE
< Alternate-Protocol: 443:quic
< Transfer-Encoding: chunked
< 
* Connection #0 to host www.googleapis.com left intact
Response [https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY_KEY]
  Status: 200
  Content-type: application/json; charset=UTF-8
{
 "kind": "qpxExpress#tripsSearch",
 "trips": {
  "kind": "qpxexpress#tripOptions",
  "requestId": "UTlu4NDcLz3Ypcicp0KKI3",
  "data": {
   "kind": "qpxexpress#data",
   "airport": [
    {
     "kind": "qpxexpress#airportData", ...

有人有运气吗?

非常感谢!

卡洛斯

1 个答案:

答案 0 :(得分:2)

如果有人有兴趣。以下是邓肯和哈德利的答案:

Windows的postForm命令如下:

library(RCurl)
library(RJSONIO)

x <- list(
      request = list(
        passengers = list(
          adultCount = 1
        ),
        slice = list(
          list(
            origin = "BOS", 
            destination = "LAX", 
            date = "2014-07-29"
          )
        ), 
        refundable = "false",
        solutions = 10
      )
    )

postForm("https://www.googleapis.com/qpxExpress/v1/trips/search?key=my_KEY",
         .opts = list(postfields = toJSON(x), 
                      cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"),
                      httpheader = c('Content-Type' = 'application/json',
                                     Accept = 'application/json'),
                      verbose = TRUE
                  ))

您可以省略

cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl") 

如果您在Linux计算机上运行。

如果你更喜欢使用httr,这里是命令:

library(httr)

url <- "https://www.googleapis.com/qpxExpress/v1/trips/search"
json <- jsonlite::toJSON(x, auto_unbox = TRUE)

POST(url, query = list(key = my_Key), body = json, content_type_json())

干杯。

卡洛斯