UpQuark
UpQuark

Reputation: 801

How do you open a file in C++ from HTTP where the URL is NOT the file location

I'm a first year comp sci student with a moderate knowledge of C++ and for a job I'm trying to put together a utility using a new U.S. Census Bureau API. It takes ID codes for things like state/county/census tract and the desired table and spits back out the desired table for the desired location.

Here's an example of a query for population stats for California and New York.

More examples can be found here: http://www.census.gov/developers/

My snag is that I've both never worked with files from HTTP and also I'm not sure how to handle a URL that outputs plain text but doesn't actually lead to the file location. Would it be possible to just use stdin? I don't understand how to handle the output given by one of the census query URLs.

Right now I'm using infile which I know isn't correct but I'm not sure a correct solution is either.

Thanks

Upvotes: 1

Views: 183

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490108

The fact that the data you're receiving is (apparently) generated on the fly rather than coming from a file doesn't really make any difference to you -- you get the same stream of bytes either way.

My immediate advice would be to use cURL for the job. Most of your work is generating a correct URL, which is what cURL specializes in. It'll then make it pretty easy to grab the data. From there, you can use any of quite a few JSON parser libraries (e.g., yajl), or you can parse it on your own (JSON is simple enough to make that fairly practical). A quick Google indicates that a fair number of people have already done this, and have various blog posts and such giving information about how to do it (though I suspect most of that is probably unnecessary).

Upvotes: 2

Related Questions