Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(dot-net-sdk): read MapAsync links from the v2 map response (#5208)

Erik Engervall05607690bffa
diff --git a/apps/dot-net-sdk/Firecrawl.Tests/MapTests.cs b/apps/dot-net-sdk/Firecrawl.Tests/MapTests.cs
new file mode 100644
index 000000000..212672a2f
--- /dev/null
+++ b/apps/dot-net-sdk/Firecrawl.Tests/MapTests.cs
@@ -0,0 +1,54 @@
+using System.Net;
+using System.Text;
+using System.Text.Json;
+using Firecrawl.Models;
+using Xunit;
+
+namespace Firecrawl.Tests;
+
+public class MapTests
+{
+ private sealed class StubHandler : HttpMessageHandler
+ {
+ private readonly string _body;
+
+ public StubHandler(string body) => _body = body;
+
+ public HttpRequestMessage? Request { get; private set; }
+
+ public string? RequestBody { get; private set; }
+
+ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
+ {
+ Request = request;
+ RequestBody = request.Content is null ? null : await request.Content.ReadAsStringAsync(cancellationToken);
+ return new HttpResponseMessage(HttpStatusCode.OK)
+ {
+ Content = new StringContent(_body, Encoding.UTF8, "application/json"),
+ };
+ }
+ }
+
+ [Fact]
+ public async Task MapAsync_ReadsTopLevelLinkObjects()
+ {
+ var handler = new StubHandler(
+ "{\"success\":true,\"links\":[{\"url\":\"https://example.com/pricing\",\"title\":\"Pricing\",\"description\":\"Plans\"},{\"url\":\"https://example.com/about\"}]}");
+ var client = new FirecrawlClient(apiKey: "fc-test-key", apiUrl: "http://localhost", maxRetries: 0, httpClient: new HttpClient(handler));
+
+ var data = await client.MapAsync("https://example.com", new MapOptions { Limit = 2 });
+
+ Assert.NotNull(data.Links);
+ Assert.Equal(2, data.Links!.Count);
+ Assert.Equal("https://example.com/pricing", data.Links[0].Url);
+ Assert.Equal("Pricing", data.Links[0].Title);
+ Assert.Equal("Plans", data.Links[0].Description);
+ Assert.Equal("https://example.com/about", data.Links[1].Url);
+ Assert.Null(data.Links[1].Title);
+
+ Assert.Equal("http://localhost/v2/map", handler.Request!.RequestUri!.ToString());
+ using var body = JsonDocument.Parse(handler.RequestBody!);
+ Assert.Equal("https://example.com", body.RootElement.GetProperty("url").GetString());
+ Assert.Equal(2, body.RootElement.GetProperty("limit").GetInt32());
+ }
+}
diff --git a/apps/dot-net-sdk/Firecrawl/FirecrawlClient.cs b/apps/dot-net-sdk/Firecrawl/FirecrawlClient.cs
index d1cec4df0..cacab58fd 100644
--- a/apps/dot-net-sdk/Firecrawl/FirecrawlClient.cs
+++ b/apps/dot-net-sdk/Firecrawl/FirecrawlClient.cs
@@ -334,10 +334,8 @@ public class FirecrawlClient
var body = BuildBody(options);
body["url"] = url;
- var response = await _http.PostAsync<ApiResponse<MapData>>(
+ return await _http.PostAsync<MapData>(
"/v2/map", body, cancellationToken: cancellationToken);
-
- return response.Data ?? throw new FirecrawlException("Map response contained no data");
}
// ================================================================
diff --git a/apps/dot-net-sdk/Firecrawl/Models/MapData.cs b/apps/dot-net-sdk/Firecrawl/Models/MapData.cs
index 033ca88c8..d9adb4cf6 100644
--- a/apps/dot-net-sdk/Firecrawl/Models/MapData.cs
+++ b/apps/dot-net-sdk/Firecrawl/Models/MapData.cs
@@ -8,5 +8,20 @@ namespace Firecrawl.Models;
public class MapData
{
[JsonPropertyName("links")]
- public List<string>? Links { get; set; }
+ public List<MapLink>? Links { get; set; }
+}
+
+/// <summary>
+/// A URL discovered by map, with the page title and description when known.
+/// </summary>
+public class MapLink
+{
+ [JsonPropertyName("url")]
+ public string? Url { get; set; }
+
+ [JsonPropertyName("title")]
+ public string? Title { get; set; }
+
+ [JsonPropertyName("description")]
+ public string? Description { get; set; }
}
diff --git a/apps/dot-net-sdk/README.md b/apps/dot-net-sdk/README.md
index 2ac2986e8..85ec78618 100644
--- a/apps/dot-net-sdk/README.md
+++ b/apps/dot-net-sdk/README.md
@@ -223,9 +223,9 @@ var data = await client.MapAsync("https://example.com",
Limit = 100
});
-foreach (var url in data.Links!)
+foreach (var link in data.Links!)
{
- Console.WriteLine(url);
+ Console.WriteLine($"{link.Url} {link.Title}");
}
```