0

I am trying to use R with Hotwire Hotels API: Below is the original request:

getURL(http://api.hotwire.com/v1/deal/hotel?dest=chicago&apikey=##############&format=JSON&limit=50&startdate=08/27/2015&enddate=08/28/2015)

However, when I try to pass the ‘startdateand ‘enddate as parameters (see below), it does not seem to pick the date.

getURL ("http://api.hotwire.com/v1/deal/hotel?dest=chicago&apikey=##############&format=JSON&limit=50",     
 httpheader=list(startdate=c_in,enddate=c_out))

Would appreciate any help on this.

Subra

2
  • 1
    What are c_in and c_out? Are they variables defined in your global environment? Commented Aug 2, 2015 at 5:04
  • Couldn't you just use paste0() to build the URL for the call to getURL() with the dates based on c_in and c_out, like getURL(paste0("http://api.hotwire.com/v1/deal/hotel?dest=chicago&apikey=##############&format=JSON&limit=50&startdate=", c_in, "&enddate=", c_out))? Commented Aug 2, 2015 at 10:34

2 Answers 2

4

You should really try to switch to httr as it makes all the web calls much cleaner (and the API docs say the date params go in the query part):

library(httr)

c_in <- '08/02/2015'
c_out <- '08/05/2015'

hotwire_api_url <- "http://api.hotwire.com/v1/deal/hotel"

res <- GET(hotwire_api_url,
           query=list(dest="chicago",
                      apikey="########",
                      format="JSON",
                      limit=50,
                      startdate=c_in,
                      enddate=c_out))

# since return val is "text/x-json"

recs <- jsonlite::fromJSON(content(res, as="text"))

head(recs$Result)
##                   FoundDate CurrencyCode NightDuration    EndDate
## 1 2015-08-01T23:38:48-07:00          USD           3.0 08/05/2015
## 2 2015-08-01T23:38:04-07:00          USD           3.0 08/05/2015
## 3 2015-08-01T23:38:47-07:00          USD           3.0 08/05/2015
## 4 2015-08-01T23:38:48-07:00          USD           3.0 08/05/2015
## 5 2015-08-01T23:38:36-07:00          USD           3.0 08/05/2015
## 6 2015-08-01T23:38:18-07:00          USD           3.0 08/05/2015
##                                 Headline IsWeekendStay Price  StartDate
## 1    Naperville 5 Star Hotel, $143/night         false 143.0 08/02/2015
## 2     Northbrook 4 Star Hotel, $67/night         false  67.0 08/02/2015
## 3     Chicago 4.5 Star Hotel, $208/night         false 208.0 08/02/2015
## 4 Schiller Park 4 Star Hotel, $140/night         false 140.0 08/02/2015
## 5     Chicago 4.5 Star Hotel, $142/night         false 142.0 08/02/2015
## 6     Naperville 4 Star Hotel, $98/night         false  98.0 08/02/2015
##                                                                                                                                                                                                                 Url
## 1     http://www.hotwire.com/hotel/superPage.jsp?encDealHash=MTAwOjE4NTAzOjE0MDUwOjUuMDoxNDMuMDpZOlk6WQ--&rs=20500&xid=x-103&wid=w-3&rid=r-69820702248&startDate=08/02/2015&endDate=08/05/2015&bid=B311402&sid=S298
## 2         http://www.hotwire.com/hotel/superPage.jsp?encDealHash=MTAwOjM1MTEwOjg3NTQ3OjQuMDo2Ny4wOlk6WTpZ&rs=20500&xid=x-103&wid=w-3&rid=r-69820702248&startDate=08/02/2015&endDate=08/05/2015&bid=B311402&sid=S298
## 3 http://www.hotwire.com/hotel/superPage.jsp?encDealHash=MTAwOjQ4Njk6ODc1NDU6NC41OjIwOC4wMDAwMjpZOlk6WQ--&rs=20500&xid=x-103&wid=w-3&rid=r-69820702248&startDate=08/02/2015&endDate=08/05/2015&bid=B311402&sid=S298
## 4     http://www.hotwire.com/hotel/superPage.jsp?encDealHash=MTAwOjM1MTI3Ojg5ODg5OjQuMDoxNDAuMDpZOlk6WQ--&rs=20500&xid=x-103&wid=w-3&rid=r-69820702248&startDate=08/02/2015&endDate=08/05/2015&bid=B311402&sid=S298
## 5         http://www.hotwire.com/hotel/superPage.jsp?encDealHash=MTAwOjQ4Njk6ODc5NTE6NC41OjE0Mi4wOlk6WTpZ&rs=20500&xid=x-103&wid=w-3&rid=r-69820702248&startDate=08/02/2015&endDate=08/05/2015&bid=B311402&sid=S298
## 6         http://www.hotwire.com/hotel/superPage.jsp?encDealHash=MTAwOjE4NTAzOjE0MDUwOjQuMDo5OC4wOlk6WTpZ&rs=20500&xid=x-103&wid=w-3&rid=r-69820702248&startDate=08/02/2015&endDate=08/05/2015&bid=B311402&sid=S298
##            City CountryCode NeighborhoodLatitude NeighborhoodLongitude
## 1    Naperville          US              41.8008              -88.1328
## 2    Northbrook          US              42.1138              -87.8601
## 3       Chicago          US              41.8989              -87.6243
## 4 Schiller Park          US              41.9334              -87.9029
## 5       Chicago          US              41.8549              -87.6221
## 6    Naperville          US              41.8008              -88.1328
##                                   Neighborhood StarRating StateCode
## 1                           Lisle - Naperville        5.0        IL
## 2         Northbrook - Glenview - Mt. Prospect        4.0        IL
## 3         Magnificent Mile North - Water Tower        4.5        IL
## 4                O'Hare Intl Airport ORD South        4.0        IL
## 5 McCormick Place - South Loop - Soldier Field        4.5        IL
## 6                           Lisle - Naperville        4.0        IL
Sign up to request clarification or add additional context in comments.

Comments

2

httpheader param is for passing headers, not query params

using RCurl::getForm

getForm("http://httpbin.org/get", .params = c(q = "ham"))

Or using httr::GET

GET("http://httpbin.org/get", query = list(q = "ham"))

1 Comment

It still does not pick the dates. I am starting to think that it has to do something with how the parameters are passed in the original query. ** "api.hotwire.com/v1/deal/… &startdate=08/24/2015 & enddate=08/25/2015".**

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.