How to build a request for an API
I'm trying to make a request to an API using what we saw on the "Convert Our Request to a Script" module of the last course.
The API I'm trying to fetch data from, asks to provide the basic information to access the REST API in the "header part" of the request.
The basic info they ask for is "Authentication" & "Accept" (to request JSON)
If I use a Chrome extension such as Advanced Rest Client, I manage to get a 200 response with what I want (http://snag.gy/MKc5Q.jpg), however I'm having troubles in generating the script since I still don't understand how to provide the basic info in the header part (as oppossed to doing it as a query parameter in the URL)
Is there any particular thing I'd need to do to adapt the "craigslist_scraper.rb" script to work for this case?
So far, I've got something like:
/// -------------------- ///
authentication = 'user&password'
request_url = "https://partner.ws.qa1.domain.com/postings?checkIn=20151001&checkOut=20151003&locId=509&room1=1,1,3&room2=2,0,3&room3=0,1,4&pos=HCL&rateDetails=all&contentDetails=all&include=taxesAndFees"
# Prepare API request
uri = URI.parse(request_url)
#uri.query = URI.encode_www_form(params)
# Submit request
result = open(uri).read
# Display results to screen
/// --------------- ///
Posted about 4 years ago by