-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add /pdf-with-added-attachment Java sample
- Loading branch information
1 parent
a44c8b9
commit cbbc094
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import io.github.cdimascio.dotenv.Dotenv; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import okhttp3.MediaType; | ||
import okhttp3.MultipartBody; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
import org.json.JSONObject; | ||
|
||
public class PdfWithAddedAttachment { | ||
|
||
// Specify the path to your file here, or as the first argument when running the program. | ||
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf"; | ||
|
||
// Specify the path to your file attachment here, or as the second argument when running the program. | ||
private static final String DEFAULT_ATTACHMENT_PATH = "/path/to/file.xml"; | ||
|
||
// Specify your API key here, or in the environment variable PDFREST_API_KEY. | ||
// You can also put the environment variable in a .env file. | ||
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; | ||
|
||
public static void main(String[] args) { | ||
File inputFile, attachmentFile; | ||
if (args.length > 1) { | ||
inputFile = new File(args[0]); | ||
attachmentFile = new File(args[1]); | ||
} else { | ||
inputFile = new File(DEFAULT_FILE_PATH); | ||
attachmentFile = new File(DEFAULT_ATTACHMENT_PATH); | ||
} | ||
|
||
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); | ||
|
||
final RequestBody inputFileRequestBody = | ||
RequestBody.create(inputFile, MediaType.parse("application/pdf")); | ||
final RequestBody attachmentFileRequestBody = | ||
RequestBody.create(attachmentFile, MediaType.parse("application/xml")); | ||
RequestBody requestBody = | ||
new MultipartBody.Builder() | ||
.setType(MultipartBody.FORM) | ||
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody) | ||
.addFormDataPart("file_to_attach", attachmentFile.getName(), attachmentFileRequestBody) | ||
.build(); | ||
Request request = | ||
new Request.Builder() | ||
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) | ||
.url("https://api.pdfrest.com/pdf-with-added-attachment") | ||
.post(requestBody) | ||
.build(); | ||
try { | ||
OkHttpClient client = new OkHttpClient().newBuilder().build(); | ||
Response response = client.newCall(request).execute(); | ||
System.out.println("Result code " + response.code()); | ||
if (response.body() != null) { | ||
System.out.println(prettyJson(response.body().string())); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static String prettyJson(String json) { | ||
// https://stackoverflow.com/a/9583835/11996393 | ||
return new JSONObject(json).toString(4); | ||
} | ||
} |