I am receiving the following response header from an API request (as String):
<https://example1>; rel="previous", <https://example2>; rel="next"
I need to capture the url that relates only to the "next" rel attribute.
I know I can dig that url out of the string as is but I cannot guarantee they will be returned in the order prev, next.
What is a good way to ensure I capture the correct url that is associated with the next param?
I would prefer to know how to do this in vb but am happy to translate a C# answer.
>Solution :
Looking at your example shows me that the "parts" of previous and next are separated by a comma, so we can split the string into two at the comma, find which one contains rel="next", then substring inside it to get the URL, something like
const string response = "<https://example1>; rel=\"previous\", <https://example2>; rel=\"next\"";
var split = response.Split(',');
var url = string.Empty;
foreach (var part in split)
{
var trimmed = part.Trim();
if (!trimmed.Contains("rel=\"next\""))
continue;
url = trimmed.Substring(0, trimmed.IndexOf(';'));
url = url.TrimStart('<');
url = url.TrimEnd('>');
}
Console.WriteLine(url);
Check out the demo