Skip to content

Commit ff90d69

Browse files
Add multipart examples to C# samples with standardized headers and improved error handling
- Introduced multipart handling for multiple formats: PNG, JPG, GIF, BMP, TIF, and Office files. - Enhanced headers to include purpose, setup, usage, and GDPR API details. - Standardized input validation, environment variable usage, and API key handling. - Updated `Program.cs` to route new multipart commands.
1 parent 9671577 commit ff90d69

17 files changed

Lines changed: 931 additions & 219 deletions

File tree

DotNET/DotNetSamples.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@
6565
<Compile Include="Endpoint Examples/JSON Payload/signed-pdf.cs" />
6666
<Compile Include="Endpoint Examples/JSON Payload/signed-pdf-non-pfx.cs" />
6767
<Compile Include="Endpoint Examples/Multipart Payload/pdf.cs" />
68+
<Compile Include="Endpoint Examples/Multipart Payload/png.cs" />
69+
<Compile Include="Endpoint Examples/Multipart Payload/jpg.cs" />
70+
<Compile Include="Endpoint Examples/Multipart Payload/gif.cs" />
71+
<Compile Include="Endpoint Examples/Multipart Payload/bmp.cs" />
72+
<Compile Include="Endpoint Examples/Multipart Payload/tif.cs" />
73+
<Compile Include="Endpoint Examples/Multipart Payload/upload.cs" />
74+
<Compile Include="Endpoint Examples/Multipart Payload/get-resource.cs" />
75+
<Compile Include="Endpoint Examples/Multipart Payload/delete-resource.cs" />
76+
<Compile Include="Endpoint Examples/Multipart Payload/batch-delete.cs" />
77+
<Compile Include="Endpoint Examples/Multipart Payload/merged-pdf.cs" />
78+
<Compile Include="Endpoint Examples/Multipart Payload/zip.cs" />
79+
<Compile Include="Endpoint Examples/Multipart Payload/unzip.cs" />
6880
<Compile Include="Complex Flow Examples/merge-different-file-types.cs" />
81+
<Compile Include="Endpoint Examples/Multipart Payload/excel.cs" />
82+
<Compile Include="Endpoint Examples/Multipart Payload/powerpoint.cs" />
83+
<Compile Include="Endpoint Examples/Multipart Payload/word.cs" />
84+
</ItemGroup>
85+
<ItemGroup>
86+
<Folder Include="Complex Flow Examples\" />
6987
</ItemGroup>
7088
</Project>
Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,47 @@
1-
var client = new HttpClient();
2-
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.pdfrest.com/delete");
3-
request.Headers.Add("api-key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
4-
var content = new MultipartFormDataContent();
5-
content.Add(new StringContent("xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), "ids");
6-
request.Content = content;
7-
var response = await client.SendAsync(request);
8-
response.EnsureSuccessStatusCode();
9-
Console.WriteLine(await response.Content.ReadAsStringAsync());
1+
/*
2+
* What this sample does:
3+
* - Deletes multiple resources by ids.
4+
* - Routed from Program.cs as: `dotnet run -- batch-delete-multipart <id1> [id2] [...]`.
5+
*
6+
* Setup (environment):
7+
* - Copy .env.example to .env
8+
* - Set PDFREST_API_KEY=your_api_key_here
9+
* - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use:
10+
* PDFREST_URL=https://eu-api.pdfrest.com
11+
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
12+
*/
13+
14+
namespace Samples.EndpointExamples.MultipartPayload
15+
{
16+
public static class BatchDelete
17+
{
18+
public static async Task Execute(string[] args)
19+
{
20+
if (args == null || args.Length < 1)
21+
{
22+
Console.Error.WriteLine("batch-delete-multipart requires <id1> [id2] [...]");
23+
Environment.Exit(1);
24+
return;
25+
}
26+
27+
var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
28+
if (string.IsNullOrWhiteSpace(apiKey))
29+
{
30+
Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY");
31+
Environment.Exit(1);
32+
return;
33+
}
34+
var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";
35+
36+
using var client = new HttpClient();
37+
using var request = new HttpRequestMessage(HttpMethod.Post, baseUrl.TrimEnd('/') + "/delete");
38+
request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
39+
var content = new MultipartFormDataContent();
40+
content.Add(new StringContent(string.Join(',', args)), "ids");
41+
request.Content = content;
42+
var response = await client.SendAsync(request);
43+
var body = await response.Content.ReadAsStringAsync();
44+
Console.WriteLine(body);
45+
}
46+
}
47+
}
Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,68 @@
1+
/*
2+
* What this sample does:
3+
* - Converts an input file to BMP using multipart/form-data.
4+
* - Routed from Program.cs as: `dotnet run -- bmp-multipart <inputFile>`.
5+
*
6+
* Setup (environment):
7+
* - Copy .env.example to .env
8+
* - Set PDFREST_API_KEY=your_api_key_here
9+
* - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use:
10+
* PDFREST_URL=https://eu-api.pdfrest.com
11+
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
12+
*/
13+
114
using System.Text;
215

3-
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
16+
namespace Samples.EndpointExamples.MultipartPayload
417
{
5-
using (var request = new HttpRequestMessage(HttpMethod.Post, "bmp"))
18+
public static class Bmp
619
{
7-
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
8-
request.Headers.Accept.Add(new("application/json"));
9-
var multipartContent = new MultipartFormDataContent();
20+
public static async Task Execute(string[] args)
21+
{
22+
if (args == null || args.Length < 1)
23+
{
24+
Console.Error.WriteLine("bmp-multipart requires <inputFile>");
25+
Environment.Exit(1);
26+
return;
27+
}
28+
29+
var inputPath = args[0];
30+
if (!File.Exists(inputPath))
31+
{
32+
Console.Error.WriteLine($"File not found: {inputPath}");
33+
Environment.Exit(1);
34+
return;
35+
}
36+
37+
var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
38+
if (string.IsNullOrWhiteSpace(apiKey))
39+
{
40+
Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY");
41+
Environment.Exit(1);
42+
return;
43+
}
1044

11-
var byteArray = File.ReadAllBytes("/path/to/file");
12-
var byteAryContent = new ByteArrayContent(byteArray);
13-
multipartContent.Add(byteAryContent, "file", "file_name");
14-
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");
45+
var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";
1546

47+
using (var httpClient = new HttpClient { BaseAddress = new Uri(baseUrl) })
48+
using (var request = new HttpRequestMessage(HttpMethod.Post, "bmp"))
49+
{
50+
request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
51+
request.Headers.Accept.Add(new("application/json"));
52+
var multipartContent = new MultipartFormDataContent();
1653

17-
request.Content = multipartContent;
18-
var response = await httpClient.SendAsync(request);
54+
var byteArray = File.ReadAllBytes(inputPath);
55+
var byteAryContent = new ByteArrayContent(byteArray);
56+
multipartContent.Add(byteAryContent, "file", Path.GetFileName(inputPath));
57+
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
1958

20-
var apiResult = await response.Content.ReadAsStringAsync();
59+
request.Content = multipartContent;
60+
var response = await httpClient.SendAsync(request);
61+
var apiResult = await response.Content.ReadAsStringAsync();
2162

22-
Console.WriteLine("API response received.");
23-
Console.WriteLine(apiResult);
63+
Console.WriteLine("API response received.");
64+
Console.WriteLine(apiResult);
65+
}
66+
}
2467
}
2568
}
Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,46 @@
1-
var client = new HttpClient();
2-
var request = new HttpRequestMessage(HttpMethod.Delete, "https://api.pdfrest.com/resource/xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
3-
request.Headers.Add("api-key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
4-
var response = await client.SendAsync(request);
5-
response.EnsureSuccessStatusCode();
6-
Console.WriteLine(await response.Content.ReadAsStringAsync());
1+
/*
2+
* What this sample does:
3+
* - Deletes a resource by id.
4+
* - Routed from Program.cs as: `dotnet run -- delete-resource-multipart <id>`.
5+
*
6+
* Setup (environment):
7+
* - Copy .env.example to .env
8+
* - Set PDFREST_API_KEY=your_api_key_here
9+
* - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use:
10+
* PDFREST_URL=https://eu-api.pdfrest.com
11+
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
12+
*/
13+
14+
namespace Samples.EndpointExamples.MultipartPayload
15+
{
16+
public static class DeleteResource
17+
{
18+
public static async Task Execute(string[] args)
19+
{
20+
if (args == null || args.Length < 1)
21+
{
22+
Console.Error.WriteLine("delete-resource-multipart requires <id>");
23+
Environment.Exit(1);
24+
return;
25+
}
26+
27+
var id = args[0];
28+
var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
29+
if (string.IsNullOrWhiteSpace(apiKey))
30+
{
31+
Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY");
32+
Environment.Exit(1);
33+
return;
34+
}
35+
var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";
36+
var url = baseUrl.TrimEnd('/') + "/resource/" + id;
37+
38+
using var client = new HttpClient();
39+
using var request = new HttpRequestMessage(HttpMethod.Delete, url);
40+
request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
41+
var response = await client.SendAsync(request);
42+
var body = await response.Content.ReadAsStringAsync();
43+
Console.WriteLine(body);
44+
}
45+
}
46+
}
Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,68 @@
1+
/*
2+
* What this sample does:
3+
* - Converts an input file to Excel via multipart/form-data.
4+
* - Routed from Program.cs as: `dotnet run -- excel-multipart <inputFile>`.
5+
*
6+
* Setup (environment):
7+
* - Copy .env.example to .env
8+
* - Set PDFREST_API_KEY=your_api_key_here
9+
* - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use:
10+
* PDFREST_URL=https://eu-api.pdfrest.com
11+
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
12+
*/
13+
114
using System.Text;
215

3-
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
16+
namespace Samples.EndpointExamples.MultipartPayload
417
{
5-
using (var request = new HttpRequestMessage(HttpMethod.Post, "excel"))
18+
public static class Excel
619
{
7-
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
8-
request.Headers.Accept.Add(new("application/json"));
9-
var multipartContent = new MultipartFormDataContent();
20+
public static async Task Execute(string[] args)
21+
{
22+
if (args == null || args.Length < 1)
23+
{
24+
Console.Error.WriteLine("excel-multipart requires <inputFile>");
25+
Environment.Exit(1);
26+
return;
27+
}
28+
29+
var inputPath = args[0];
30+
if (!File.Exists(inputPath))
31+
{
32+
Console.Error.WriteLine($"File not found: {inputPath}");
33+
Environment.Exit(1);
34+
return;
35+
}
36+
37+
var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
38+
if (string.IsNullOrWhiteSpace(apiKey))
39+
{
40+
Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY");
41+
Environment.Exit(1);
42+
return;
43+
}
1044

11-
var byteArray = File.ReadAllBytes("/path/to/file");
12-
var byteAryContent = new ByteArrayContent(byteArray);
13-
multipartContent.Add(byteAryContent, "file", "file_name");
14-
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");
45+
var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";
1546

47+
using (var httpClient = new HttpClient { BaseAddress = new Uri(baseUrl) })
48+
using (var request = new HttpRequestMessage(HttpMethod.Post, "excel"))
49+
{
50+
request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
51+
request.Headers.Accept.Add(new("application/json"));
52+
var multipartContent = new MultipartFormDataContent();
1653

17-
request.Content = multipartContent;
18-
var response = await httpClient.SendAsync(request);
54+
var byteArray = File.ReadAllBytes(inputPath);
55+
var byteAryContent = new ByteArrayContent(byteArray);
56+
multipartContent.Add(byteAryContent, "file", Path.GetFileName(inputPath));
57+
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
1958

20-
var apiResult = await response.Content.ReadAsStringAsync();
59+
request.Content = multipartContent;
60+
var response = await httpClient.SendAsync(request);
61+
var apiResult = await response.Content.ReadAsStringAsync();
2162

22-
Console.WriteLine("API response received.");
23-
Console.WriteLine(apiResult);
63+
Console.WriteLine("API response received.");
64+
Console.WriteLine(apiResult);
65+
}
66+
}
2467
}
2568
}
Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,66 @@
1-
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com/resource/") } )
1+
/*
2+
* What this sample does:
3+
* - Downloads a resource file by id.
4+
* - Routed from Program.cs as: `dotnet run -- get-resource-multipart <id> [out]`.
5+
*
6+
* Setup (environment):
7+
* - Copy .env.example to .env
8+
* - Set PDFREST_API_KEY=your_api_key_here
9+
* - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use:
10+
* PDFREST_URL=https://eu-api.pdfrest.com
11+
* For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
12+
*/
13+
14+
namespace Samples.EndpointExamples.MultipartPayload
215
{
3-
try
16+
public static class GetResource
417
{
5-
string id = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // ID to retrieve
6-
7-
using (var stream = await httpClient.GetStreamAsync(id + "?format=file"))
18+
public static async Task Execute(string[] args)
819
{
9-
using (var fs = new FileStream("/path/to/save/file", FileMode.CreateNew))
20+
if (args == null || args.Length < 1)
21+
{
22+
Console.Error.WriteLine("get-resource-multipart requires <id> [out]");
23+
Environment.Exit(1);
24+
return;
25+
}
26+
27+
var id = args[0];
28+
var outPath = args.Length > 1 ? args[1] : "resource.bin";
29+
30+
var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
31+
if (string.IsNullOrWhiteSpace(apiKey))
1032
{
11-
await stream.CopyToAsync(fs);
33+
Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY");
34+
Environment.Exit(1);
35+
return;
36+
}
37+
38+
var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";
39+
var resourceBase = baseUrl.TrimEnd('/') + "/resource/";
40+
41+
using (var httpClient = new HttpClient { BaseAddress = new Uri(resourceBase) })
42+
{
43+
try
44+
{
45+
using (var request = new HttpRequestMessage(HttpMethod.Get, id + "?format=file"))
46+
{
47+
request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
48+
using (var response = await httpClient.SendAsync(request))
49+
{
50+
response.EnsureSuccessStatusCode();
51+
await using var stream = await response.Content.ReadAsStreamAsync();
52+
await using var fs = new FileStream(outPath, FileMode.Create);
53+
await stream.CopyToAsync(fs);
54+
}
55+
}
56+
Console.WriteLine($"Saved to {outPath}");
57+
}
58+
catch (HttpRequestException e)
59+
{
60+
Console.Error.WriteLine($"HTTP error: {e.Message}");
61+
Environment.Exit(1);
62+
}
1263
}
1364
}
1465
}
15-
catch (HttpRequestException e)
16-
{
17-
Console.WriteLine("Message :{0} ", e.Message);
18-
}
1966
}

0 commit comments

Comments
 (0)