Skip to content

Commit

Permalink
Add upload + raw JSON DotNet samples
Browse files Browse the repository at this point in the history
  • Loading branch information
datalogics-tsmith committed Nov 17, 2023
1 parent 026ca08 commit 379378e
Show file tree
Hide file tree
Showing 34 changed files with 1,736 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DotNET/Endpoint Examples/JSON Payload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This directory contains examples of how to call each endpoint of the
PdfRest API using DotNet. Please refer to https://pdfrest.com/documentation.html
for accepted inputs, options, and other specifics about individual endpoints.
50 changes: 50 additions & 0 deletions DotNET/Endpoint Examples/JSON Payload/bmp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

using Newtonsoft.Json.Linq;
using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
using (var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload"))
{
uploadRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
uploadRequest.Headers.Accept.Add(new("application/json"));

var uploadByteArray = File.ReadAllBytes("/path/to/file");
var uploadByteAryContent = new ByteArrayContent(uploadByteArray);
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf");


uploadRequest.Content = uploadByteAryContent;
var uploadResponse = await httpClient.SendAsync(uploadRequest);

var uploadResult = await uploadResponse.Content.ReadAsStringAsync();

Console.WriteLine("Upload response received.");
Console.WriteLine(uploadResult);

JObject uploadResultJson = JObject.Parse(uploadResult);
var uploadedID = uploadResultJson["files"][0]["id"];
using (var bmpRequest = new HttpRequestMessage(HttpMethod.Post, "bmp"))
{
bmpRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
bmpRequest.Headers.Accept.Add(new("application/json"));

bmpRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");


JObject parameterJson = new JObject
{
["id"] = uploadedID,
};

bmpRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json"); ;
var bmpResponse = await httpClient.SendAsync(bmpRequest);

var bmpResult = await bmpResponse.Content.ReadAsStringAsync();

Console.WriteLine("Processing response received.");
Console.WriteLine(bmpResult);
}
}
}
51 changes: 51 additions & 0 deletions DotNET/Endpoint Examples/JSON Payload/compressed-pdf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

using Newtonsoft.Json.Linq;
using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
using (var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload"))
{
uploadRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
uploadRequest.Headers.Accept.Add(new("application/json"));

var uploadByteArray = File.ReadAllBytes("/path/to/file");
var uploadByteAryContent = new ByteArrayContent(uploadByteArray);
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf");


uploadRequest.Content = uploadByteAryContent;
var uploadResponse = await httpClient.SendAsync(uploadRequest);

var uploadResult = await uploadResponse.Content.ReadAsStringAsync();

Console.WriteLine("Upload response received.");
Console.WriteLine(uploadResult);

JObject uploadResultJson = JObject.Parse(uploadResult);
var uploadedID = uploadResultJson["files"][0]["id"];
using (var compressRequest = new HttpRequestMessage(HttpMethod.Post, "compressed-pdf"))
{
compressRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
compressRequest.Headers.Accept.Add(new("application/json"));

compressRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");


JObject parameterJson = new JObject
{
["id"] = uploadedID,
["compression_level"] = "medium"
};

compressRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json"); ;
var compressResponse = await httpClient.SendAsync(compressRequest);

var compressResult = await compressResponse.Content.ReadAsStringAsync();

Console.WriteLine("Processing response received.");
Console.WriteLine(compressResult);
}
}
}
51 changes: 51 additions & 0 deletions DotNET/Endpoint Examples/JSON Payload/decrypted-pdf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

using Newtonsoft.Json.Linq;
using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
using (var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload"))
{
uploadRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
uploadRequest.Headers.Accept.Add(new("application/json"));

var uploadByteArray = File.ReadAllBytes("/path/to/file");
var uploadByteAryContent = new ByteArrayContent(uploadByteArray);
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf");


uploadRequest.Content = uploadByteAryContent;
var uploadResponse = await httpClient.SendAsync(uploadRequest);

var uploadResult = await uploadResponse.Content.ReadAsStringAsync();

Console.WriteLine("Upload response received.");
Console.WriteLine(uploadResult);

JObject uploadResultJson = JObject.Parse(uploadResult);
var uploadedID = uploadResultJson["files"][0]["id"];
using (var decryptRequest = new HttpRequestMessage(HttpMethod.Post, "decrypted-pdf"))
{
decryptRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
decryptRequest.Headers.Accept.Add(new("application/json"));

decryptRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");


JObject parameterJson = new JObject
{
["id"] = uploadedID,
["current_open_password"] = "password"
};

decryptRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json"); ;
var decryptResponse = await httpClient.SendAsync(decryptRequest);

var decryptResult = await decryptResponse.Content.ReadAsStringAsync();

Console.WriteLine("Processing response received.");
Console.WriteLine(decryptResult);
}
}
}
51 changes: 51 additions & 0 deletions DotNET/Endpoint Examples/JSON Payload/encrypted-pdf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

using Newtonsoft.Json.Linq;
using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
using (var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload"))
{
uploadRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
uploadRequest.Headers.Accept.Add(new("application/json"));

var uploadByteArray = File.ReadAllBytes("/path/to/file");
var uploadByteAryContent = new ByteArrayContent(uploadByteArray);
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf");


uploadRequest.Content = uploadByteAryContent;
var uploadResponse = await httpClient.SendAsync(uploadRequest);

var uploadResult = await uploadResponse.Content.ReadAsStringAsync();

Console.WriteLine("Upload response received.");
Console.WriteLine(uploadResult);

JObject uploadResultJson = JObject.Parse(uploadResult);
var uploadedID = uploadResultJson["files"][0]["id"];
using (var encryptRequest = new HttpRequestMessage(HttpMethod.Post, "encrypted-pdf"))
{
encryptRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
encryptRequest.Headers.Accept.Add(new("application/json"));

encryptRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");


JObject parameterJson = new JObject
{
["id"] = uploadedID,
["new_open_password"] = "password"
};

encryptRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json"); ;
var encryptResponse = await httpClient.SendAsync(encryptRequest);

var encryptResult = await encryptResponse.Content.ReadAsStringAsync();

Console.WriteLine("Processing response received.");
Console.WriteLine(encryptResult);
}
}
}
50 changes: 50 additions & 0 deletions DotNET/Endpoint Examples/JSON Payload/excel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

using Newtonsoft.Json.Linq;
using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
using (var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload"))
{
uploadRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
uploadRequest.Headers.Accept.Add(new("application/json"));

var uploadByteArray = File.ReadAllBytes("/path/to/file");
var uploadByteAryContent = new ByteArrayContent(uploadByteArray);
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf");


uploadRequest.Content = uploadByteAryContent;
var uploadResponse = await httpClient.SendAsync(uploadRequest);

var uploadResult = await uploadResponse.Content.ReadAsStringAsync();

Console.WriteLine("Upload response received.");
Console.WriteLine(uploadResult);

JObject uploadResultJson = JObject.Parse(uploadResult);
var uploadedID = uploadResultJson["files"][0]["id"];
using (var excelRequest = new HttpRequestMessage(HttpMethod.Post, "excel"))
{
excelRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
excelRequest.Headers.Accept.Add(new("application/json"));

excelRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");


JObject parameterJson = new JObject
{
["id"] = uploadedID,
};

excelRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json"); ;
var excelResponse = await httpClient.SendAsync(excelRequest);

var excelResult = await excelResponse.Content.ReadAsStringAsync();

Console.WriteLine("Processing response received.");
Console.WriteLine(excelResult);
}
}
}
51 changes: 51 additions & 0 deletions DotNET/Endpoint Examples/JSON Payload/exported-form-data.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

using Newtonsoft.Json.Linq;
using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
using (var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload"))
{
uploadRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
uploadRequest.Headers.Accept.Add(new("application/json"));

var uploadByteArray = File.ReadAllBytes("/path/to/file");
var uploadByteAryContent = new ByteArrayContent(uploadByteArray);
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf");


uploadRequest.Content = uploadByteAryContent;
var uploadResponse = await httpClient.SendAsync(uploadRequest);

var uploadResult = await uploadResponse.Content.ReadAsStringAsync();

Console.WriteLine("Upload response received.");
Console.WriteLine(uploadResult);

JObject uploadResultJson = JObject.Parse(uploadResult);
var uploadedID = uploadResultJson["files"][0]["id"];
using (var exportRequest = new HttpRequestMessage(HttpMethod.Post, "exported-form-data"))
{
exportRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
exportRequest.Headers.Accept.Add(new("application/json"));

exportRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");


JObject parameterJson = new JObject
{
["id"] = uploadedID,
["data_format"] = "xml",
};

exportRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json"); ;
var exportResponse = await httpClient.SendAsync(exportRequest);

var exportResult = await exportResponse.Content.ReadAsStringAsync();

Console.WriteLine("Processing response received.");
Console.WriteLine(exportResult);
}
}
}
50 changes: 50 additions & 0 deletions DotNET/Endpoint Examples/JSON Payload/extracted-text.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

using Newtonsoft.Json.Linq;
using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
using (var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload"))
{
uploadRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
uploadRequest.Headers.Accept.Add(new("application/json"));

var uploadByteArray = File.ReadAllBytes("/path/to/file");
var uploadByteAryContent = new ByteArrayContent(uploadByteArray);
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf");


uploadRequest.Content = uploadByteAryContent;
var uploadResponse = await httpClient.SendAsync(uploadRequest);

var uploadResult = await uploadResponse.Content.ReadAsStringAsync();

Console.WriteLine("Upload response received.");
Console.WriteLine(uploadResult);

JObject uploadResultJson = JObject.Parse(uploadResult);
var uploadedID = uploadResultJson["files"][0]["id"];
using (var extractRequest = new HttpRequestMessage(HttpMethod.Post, "extracted-text"))
{
extractRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
extractRequest.Headers.Accept.Add(new("application/json"));

extractRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");


JObject parameterJson = new JObject
{
["id"] = uploadedID,
};

extractRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json"); ;
var extractResponse = await httpClient.SendAsync(extractRequest);

var extractResult = await extractResponse.Content.ReadAsStringAsync();

Console.WriteLine("Processing response received.");
Console.WriteLine(extractResult);
}
}
}
Loading

0 comments on commit 379378e

Please sign in to comment.