Stack Overflow is one of the most-used help desk in the internet that helping developers and technologists to write the scripts, examples or solutions. Their public platform serves 100 million people every month, making it one of the 50 most popular websites in the world. Their asynchronous knowledge management and collaboration offering Stack Overflow for Teams, is transforming how people work. You can ask questions on Stack Overflow for free and get answers from fellow software developers and specialists on the subject of your question.
Using an API to control Stack Overflow in your own Windows and mobile apps
Apart from the great free question and answer and answer resource there is also a Stack Exchange API. The API allows you to consume and manipulate data from the Stack Overflow site and some of their affiliates. The Stack Exchange API documentation can be found here.
In this post, we will explain how to use Stack Exchange in C++ Builder with examples.
C++ Builder and the RAD Studio IDE is the easiest and fastest C and C++ IDE & Compiler for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It’s also easy for beginners to learn with its wide range of samples, tutorials, help files and LSP support to make it easier to create and navigate your program code.
Getting a copy of C++ Builder
If you don’t currently have access to RAD Studio C++ Builder is a free C++ Builder Community Edition suitable for students, beginners and startups. You can be download if from here. There is also trial version suitable for professionals and businesses which can be downloaded from here.
RAD Studio makes using REST services really easy!
One of the useful benefits of C++ Builder it supports developing REST Client and Server applications. It has REST Components and an extremely powerful yet easy to use REST Debugger that allows you to simply connect to JSON based APIs as in Stack Exchange API.
What is REST?
REST (Representational State Transfer) is a connectivity method that allows to get and post data to create interactive applications that use Web Services. REST uses a subset of HTTP. A Web service which uses this REST data transfer architecture called as a RESTful. This web service must provide its web resources in a text form and this service also allows contents to be read and modified with a stateless protocol and a predefined set of operations. REST is a new modern data transfer architecture allows interoperability between the computer systems on the Internet that provide these services. REST is Backend as a Service (BaaS) can be used in REST Server or REST Client applications, it is an alternative to SOAP to access a Web service. The REST client tutorial shows how to use the REST Client Library for accessing REST-based web services.
In C++ and Delphi , the REST library is available for all platforms that are supported by both of them. The REST Library framework focuses on JSON as the representation format. XML is not explicitly supported. C++ Builder has REST based connectivity as a Client or as a Server that provides you to do REST connections by the REST components that comes with RAD Studio.
Using the Stack Exchange API via REST – a quick video tutorial
Before we continue with our example here’s a short video about Stack Exchange API by our own Jim McKeeth. It’s a great visual introduction to the basics of the Stack Exchange API and the RAD Studio REST Debugger Tool.
A working Stack Overflow API Example
In this example we will use the Stack Exchange API and we the ‘
questions‘ method (https://api.stackexchange.com/docs/questions) to read questions asked on the Stack Overflow website. You can query results by using filter option as explained in this picture,
- In our example, Select filter and edit option at the top right of this query panel as below,
click this filter option,
- Click unselect all from the bottom ,
- Open questions section and select question_id, body, title, view_count … or choice more or less
- Goto .wrapper group and check all from this .wrapper group
- Press save to use this custom example. Note your filter code here.
- Now we can test our filter, if you Run, you should be able to see results as below,
Setting REST Components by the REST Debugger
We can easily setup the components by using REST Debugger Tool in RAD Studio (C++ Builder or Delphi). You can launch REST Debugger from the Tools menu of RAD StudioThe Embarcadero REST Debugger(The Latest version is 10.4.2) empowers developers to explore, test, and ultimately understand how a RESTful web service works. Dive right into REST data with filterable JSON blobs, streamlined OAuth 1.0/2.0 authentication, and configurable request/resource parameters. You can directly copy and paste REST components from the REST Debugger to the RAD Studio, Delphi or C++Builder IDE. This enables configuration and consumption of REST services in Delphi or C++Builder apps with just a few clicks. REST Debugger allows you to test your connections easily, you can easily reach or transfer your data. There is also free version which can be used by other REST tools and it can be downloaded from RESTdebugger. In RAD Studio there is a good tool REST Debugger to test REST Requests (Gets, Posts, …) and it is very easy to automatically setup the REST components with their parameters.
- Run REST Debugger from Tools->Rest Debugger menu
- To GET section Type https://api.stackexchange.com/2.2/questions .This is the first part of the link! 2.2 is the version, it may be changed. Set Content-Type to application/json
- Go to Parameters Tab, Set Resource to questions
- In parameters tab Add these parameters site = stackoverflow tagged =C++Builder filter = Your Filter Code and check ‘Do Not Encode’ true. i.e. your filter code can be like !*kSMFaCHLr.6fM18X))3selIGCT)_mkJ9HtRrk6k3ThO6soFo
- Now you can your REST Request check by pressing Send Request, You will see Headers in Response section as below,
- If you received Headers information in Response section, Goto Tabular Data section and into JSON Root Element type Items to receive each columns which are our filter selections. You should be to see received data as below,
7. If all is fine, now you can copy all required REST components with their settings by pressing the Copy Components button 8. Now you can Open RAD Studio, C++ Builder (also same on Delphi) create a new project and paste these on to form 9. Arrange all on the form. You can check connection by double clicking to Request1. 10. Finally you can add StringGrid (TStringGrid) and you can bind each of these columns in BindSourceDB1 with the StringGrid1. You can execute your REST request as below,
void __fastcall TForm1::Button1Click(TObject *Sender)
Note that you can also do these settings of REST Components manually without using REST Debugger. Please check their properties. Sign up to Stack Exchange API, and try it in C++ Builder, it is also possible in Delphi too.(https://api.stackexchange.com/docs/questions)
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. There is a free C++ Builder Community Edition for students, beginners and startups, it can be downloaded from here. Professional developers can use the Professional, Architect or Enterprise versions of C++ Builder which has trial version and can be downloaded from here.
Try out the Stack Overflow API, it’s easy with RAD Studio!