-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractJson.java
More file actions
39 lines (34 loc) · 1.44 KB
/
Copy pathExtractJson.java
File metadata and controls
39 lines (34 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Extract ZUGFeRD invoice data as JSON using the InvoiceXML API.
// Get API key: https://www.invoicexml.com/account/authentication
// Docs: https://www.invoicexml.com/docs/api/extract/json
//
// Dependency: com.squareup.okhttp3:okhttp:4.12.0
import okhttp3.*;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ExtractJson {
public static void main(String[] args) throws Exception {
// Raw key only, without the "Bearer " prefix (it is added below).
String apiKey = "YOUR_API_KEY";
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "invoice.pdf",
RequestBody.create(new File("invoice.pdf"), MediaType.parse("application/pdf")))
.build();
Request request = new Request.Builder()
.url("https://api.invoicexml.com/v1/extract/json")
.header("Authorization", "Bearer " + apiKey)
.post(body)
.build();
try (Response response = new OkHttpClient().newCall(request).execute()) {
String json = response.body().string();
if (!response.isSuccessful()) {
System.err.println("InvoiceXML API error " + response.code() + ": " + json);
System.exit(1);
}
Files.write(Paths.get("invoice.json"), json.getBytes());
System.out.println(json);
}
}
}