Skip to content

Commit 3b7be1a

Browse files
committed
Added an example on hos yo use request arguments.
Closes #54
1 parent d556574 commit 3b7be1a

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

doc/Tutorial.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,56 @@ int main()
214214
clog << "Done" << endl;
215215
```
216216

217+
## Add arguments to the request-url
218+
219+
Arguments in the URL require some special care. In most situations you should
220+
use the *RequestBuilder*'s *Argument()* method for this.
221+
222+
```C++
223+
// Create an instance of the rest client
224+
auto rest_client = RestClient::Create();
225+
226+
// Create a coroutine to send our request
227+
rest_client->ProcessWithPromise([&](Context& ctx) {
228+
// This is a co-routine, running in a worker-thread
229+
230+
// Construct a request to the server
231+
auto reply = RequestBuilder(ctx)
232+
.Get("https://www.google.com/search")
233+
234+
/* Add some request arguments
235+
* Here we use Google search with arguments:
236+
*
237+
* hl=en
238+
* q=site:lastviking.eu+stbl
239+
*
240+
* which tells Google to search for 'stbl' at the site lastviking.eu,
241+
* using the English language.
242+
*
243+
* (I believe Google is recruiting their product-owners from Hell.
244+
* One of their more annoying treats is to use geo-location in
245+
* stead of browser-settings to determine the language of the
246+
* Google web pages.
247+
* Ever used Google's services from Russia? Unless you read
248+
* Russian - just don't! Or add the 'hl' argument (which is
249+
* probably what the Google developers do when they travel)
250+
*/
251+
252+
.Argument("hl", "en")
253+
.Argument("q", "site:lastviking.eu+stbl")
254+
255+
// Send the request
256+
.Execute();
257+
258+
// Fetch the payload asynchronously and send it to standard output
259+
std::cout << reply->GetBodyAsString();
260+
261+
// Return the post instance trough a C++ future<>
262+
return ;
263+
});
264+
265+
```
266+
217267
## Send a request using HTTP Basic Authentication
218268
```C++
219269

tests/functional/ReadmeTests.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,50 @@ void twelfth() {
507507
.get();
508508
}
509509

510+
void thirtheenth() {
511+
// Create an instance of the rest client
512+
auto rest_client = RestClient::Create();
513+
514+
// Create a coroutine to send our request
515+
rest_client->ProcessWithPromise([&](Context& ctx) {
516+
// This is a co-routine, running in a worker-thread
517+
518+
// Construct a request to the server
519+
auto reply = RequestBuilder(ctx)
520+
.Get("https://www.google.com/search")
521+
522+
/* Add some request arguments
523+
* Here we use Google search with arguments:
524+
*
525+
* hl=en
526+
* q=site:lastviking.eu+stbl
527+
*
528+
* which tells Google to search for 'stbl' at the site lastviking.eu,
529+
* using the English language.
530+
*
531+
* (I believe Google is recruiting their product-owners from Hell.
532+
* One of their more annoying treats is to use geo-location in
533+
* stead of browser-settings to determine the language of the
534+
* Google web pages.
535+
* Ever used Google's services from Russia? Unless you read
536+
* Russian - just don't! Or add the 'hl' argument (which is
537+
* probably what the Google developers do when they travel)
538+
*/
539+
540+
.Argument("hl", "en")
541+
.Argument("q", "site:lastviking.eu+stbl")
542+
543+
// Send the request
544+
.Execute();
545+
546+
// Fetch the payload asynchronously and send it to standard output
547+
std::cout << reply->GetBodyAsString();
548+
549+
// Return the post instance trough a C++ future<>
550+
return ;
551+
});
552+
}
553+
510554
int main() {
511555

512556
namespace logging = boost::log;
@@ -552,6 +596,9 @@ int main() {
552596
cout << "Twelfth: " << endl;
553597
twelfth();
554598

599+
cout << "Thirtheenth: " << endl;
600+
thirtheenth();
601+
555602
} catch(const exception& ex) {
556603
cerr << "Something threw up: " << ex.what() << endl;
557604
return 1;

0 commit comments

Comments
 (0)