json to std::string in c++ from an URL - c++

I want to implement something where I will get an URL which will be a link to JSON db, now I need to get the json from this URL and convert it to std:string in a c++ file. Is there any easy way to do this.

Use a library such as cURL (or one of many others available) to fetch the URL, and then store the result in an std::string.

If you are on Windows then there is no need to use cURL library - try the api call URLOpenBlockingStream. It is not hard to use and will download the JSON response from the server. JSON can be thought of as XML - it is structurally similar although there are noticeable syntactical differences. That said, it shouldn't be too hard to write a simple parser.

Related

Retrieve data from Account Server

I'm trying to make a game launcher in C++ and I was wondering if I could get some guidance on how to carry out this task. Basically I've created an API which outputs the account data in JSON format.
i.e {"success":true,"errorCode":0,"reason":"You're now logged in!"}
http_fetch("http://www.example.com/api/login.php?username="+username+"&password="+password+"");
How am I able to retrieve the data?
Sorry if you don't understand. English isn't my first language :)
-brownzilla
Look for a library that allows you to parse Json. Some examples:
Picojson
Rapidjson
Both are quite easy to use and allow you to turn json into a model that can later be used to map to your own model. Alternatively you could write your own Json parser though that would be a bit of work (reinventing the wheel perhaps).

How to load a shapefile in gwt-openlayers

We're building an application using GWT-Openlayers (not OpenLayers) and need to allow the user to load a polygon from a shapefile. Surprisingly, there doesn't seem to be an evident solution. The closest solutions are javascript libraries for interpreting shapefiles, but a javascript solution doesn't really help in a GWT application. Any recommendations?
Thanks in advance!
Lacking a simpler solution, the approach I used was as follows:
Use GWT FormPanel and FileUpload to allow user to select the file to upload
Create a custom servlet for handling the request
FormPanel sends a multipart POST of the file contents to the servlet
Servlet feeds the file content to a parser to convert to Well Known Text (WKT)
Servlet returns the WKT in the HttpResponse
Client side code converts the WKT to a gwt-openlayers vector feature and adds it to the map
Certainly not an elegant solution but seems to work. If anyone finds a better solution, it would be great to hear.

Extracting key words from HTML to C++ under linux

I am working on a simple client-server project. Client is written in Java, it sends key words to C++ server written under Linux and recives a list of URLs with best ranks ( depending on number of occurrences of key words ). Server's job is to go through some URLs in search of key words and return best-fitting URLs. And now the problem is that I have to parse HTML sites to find occurrences of key words, plus I need to extract links from visited page to search on them as well. And my question is what library can I use to do that? Remember only C++ linux libraries are suitable for me. There were some similar topics, so I tried to go through most of them, but some of libraries parse only html files and I don't want to download every site I visit, but parse it on the fly and just store it's rank and url. Some of them look a bit complicated to me - for instance firstly parsing HTML to XML or something else and then finally work on the results with C++. Is there something simple and sufficient to do what I need it to do? Any advise will be appreciated.
I don't think regular expressions are appropriate for HTML parsing. I'm using libxml2, and I enjoy it very much - easy to use, portable and lightning fast.
To get URLs from the web using C/C++ you could use the libcurl library. To parse URLs and other not too easy stuff from the site you can use a regex library.
Separating the HTML tags from the real content can also be done without the use of a library.
For more advanced stuff one could use Qt which offers classes such as QWebPage (which uses WebKit) that allows one to access the DOM-Model of the page and extract individual HTML objects (e.g. single cells of a table) rather easyly.
You can try xerces-c. It's a powerful library for xml parsing. It support xml reading on the fly, dom and sax parsing.

How to get binary post data in Django !nicely?

forgive me if this is a bit of a newbie question, I started to learn Django yesterday, and I'm trying not to get into bad habits, i.e. I am trying to do things "the django way" from the start.
I have a view that recieves binary data as a http post field. Now Django of course autoconverts my binary data to a unicode string.
My question is, how do I just get the raw binary data?
A couple of things occurred to me. Let request be the request I'm processing.
Using request.raw_post_data would involve parsing the data again - when appearantly request.POST actually stores raw data and I am actually just trying to get around the on-the-fly conversion (and besides, that is new in the development version).
Using base64 or so to transfer the data would work, but seems like too much overhead when the data transfer itself is not the problem.
doing request.encoding="foo" before getting that field (and reassigning afterwards) doesn't work either because I still get a unicode string, besides feeling like a bit of a dirty hack. Using "base64" here (not as bad as for the transfer encoding) gives me an
AssertionError.
Thanks in advance for your ideas!
EDIT:
To clarify - I am not talking about a classic file upload here, but as binary data stored in a POST field. I'd like to do it that way because the only way I want to interface with that view is via an upload script. Using a normal POST field makes both the client and the server much simpler in that case.
Some might say that storing binary data in a standard form field is a bad habit in some way :)
You could use standard library methods of Python to convert your string back to a binary representation.
Take a look at binascii — Convert between binary and ASCI
Posting before edit:
What about this piece of code (receiving data from a POST)
def handleFile(self, request):
file = request.FILES["file"]
destination = open('filename.ext', 'wb')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
Works for me.

Get a particular text from website

I'm looking for a way if you know the location where to read the text for example say, under a particular category, how would you connect to a website and search & read the text from it?
what steps do i need to follow to learn about that?
you could use libcurl/cURL for your HTML retrival
You're probably looking for a web crawler.
Here's an example of a simple crawler written in C++.
Moreover, you might want to have a look to wget, a software to retrieve files via HTTP, HTTPS and FTP.
if you are looking at a specific web-page, you could try retrieving the page and parsing it to get to the exact location you want. e.g. specific div, etc.
since you are using c++, you could try reading up on using libcurl to retrieve the information you need from the URL.
You can download an html file with WinHTTP(working example) and then search the file. There's some find algos in the std::string class for searching if your needs are relatively basic.