A query string is the part of a URL after the ?. It carries options for the path as name=value pairs, joined with &. The server reads them and shapes its answer: /prices?max_price=5 asks the supplier for the products that cost 5 or less.
You don't have to glue the query string onto the address yourself. QueryHelpers.AddQueryString takes the address, an option's name and its value, and builds the query string for you (the starting code already has using Microsoft.AspNetCore.WebUtilities;, where it lives). After the call, response.RequestMessage.RequestUri shows the address the request really went to:
var page = QueryHelpers.AddQueryString(SUPPLIER + "/catalog", "page", "2");
var reply = await http.GetAsync(page);
Console.WriteLine(reply.RequestMessage.RequestUri); // http://127.0.0.1:8001/catalog?page=2
The supplier filters its list before it answers, so the products that cost more never leave the server. For /prices with max_price set to 5, your script must print:
URL: http://127.0.0.1:8001/prices?max_price=5 Mug costs 5 Notebook costs 3 Pen costs 1
Your task: build the address of /prices with max_price set to "5" using AddQueryString, ask for it, print "URL: " and response.RequestMessage.RequestUri, then print each product that came back: its name, " costs " and its price.
Assignment
Follow the Coding Tutorial to build the query string with AddQueryString, print the URL the request went to and print the products the supplier sent back.
HintAddQueryString takes three pieces of text: the address, the name and the value, so write the 5 in quotes. The answer is a JSON array of products, so loop over products.EnumerateArray().
Free plan: 30 tutor messages. PRO is unlimited.
