Process the Response
Reference documentation on processing responses using the Mindee client libraries.
Requirements
You'll need to have already sent a file or URL as described in the Send a File or URL section.
Overview
Depending on how you've sent the file, there are two ways of obtaining the result.
If you've sent via polling (or polling and webhook) you'll get the response directly in your method call.
If you've sent only via webhook, you'll receive the response on your Web server.
Here we'll go over how you can best process the results.
Load From Webhook
If you're using the webhook pattern, you'll need to use the payload sent to your Web server.
Reading the callback data will vary greatly depending on your HTTP server. This is therefore beyond the scope of this example.
Regardless of how you access the JSON payload sent by the Mindee servers, loading this data is done by using a LocalResponse class.
Once it is loaded you can access the data in exactly the same way as a polling response.
To verify the HMAC signature, you'll need the Signing Secret from the webhook:

Assuming you're able to get the raw HTTP request via the variable request .
from mindee import LocalResponse, InferenceResponse
# Load the JSON string sent by the Mindee webhook POST callback.
local_response = LocalResponse(request.body())
# You can also load the json from a local path.
# local_response = LocalResponse("path/to/my/file.ext")
# Optionally: verify the HMAC signature
# You'll need to get the "X-Signature" custom HTTP header.
hmac_signature = request.headers.get("X-Signature")
is_valid = local_response.is_valid_hmac_signature(
    "obviously-fake-secret-key", hmac_signature
)
if not is_valid:
    raise Error("Bad HMAC signature! Is someone trying to do evil?")
# Deserialize the response into objects
response = local_response.deserialize_response(InferenceResponse)Assuming you're able to get the raw HTTP request via the variable request .
async handleMindeeResponse(data, hmacSignature) {
  const localResponse = new mindee.LocalResponse(data);
  await localResponse.init();
  const isValid = localResponse.isValidHmacSignature(
      "obviously-fake-secret-key", hmacSignature
    );
  if (!isValid) {
    throw Error("Bad HMAC signature! Is someone trying to do evil?");
  }
  const response = await localResponse.deserializeResponse(
    mindee.InferenceResponse
  );
}
// Getting the data could look something like this.
// Will vary depending on your implementation.
async handleMindeePost(request, response) {
  let body = "";
  request.on("data", function (data) {
    body += data;
  });
  req.on("end", function () {
    // Optionally: verify the HMAC signature
    // You'll need to get the "X-Signature" custom HTTP header.
    const hmacSignature = request.headers.get("X-Signature");
    
    // validate using the entire body of the response with the signature header
    await handleMindeeResponse(body, hmacSignature);
  });
}Assuming you have a Web server instance myHttpServer .
// Load the JSON string sent by the Mindee webhook POST callback.
String jsonData = myHttpServer.getPostBodyAsString();
LocalResponse localResponse = new LocalResponse(jsonData);
// Verify the HMAC signature.
// You'll need to get the "X-Signature" custom HTTP header.
String hmacSignature = myHttpServer.getHeader("X-Signature");
boolean isValid = localResponse.isValidHmacSignature(
    "obviously-fake-secret-key", hmacSignature
);
if (!isValid) {
    throw new Exception("Bad HMAC signature! Is someone trying to do evil?");
}
// You can also use a File object as the input.
//LocalResponse localResponse = new LocalResponse(
//    new File("/path/to/file.json"));
// Deserialize the response into objects
InferenceResponse response = localResponse.deserializeResponse(
    InferenceResponse.class
);
// Print a summary of the parsed data
System.out.println(response.getInference().toString());Assuming you're able to get the raw HTTP request via the variable request .
using Mindee.Parsing.V2;
public void HandleMindeeCallback(HttpRequest request)
{
    LocalResponse localResponse;
    using (var reader = new StreamReader(request.Body))
    {
        localResponse = new LocalResponse(reader.ReadToEnd());
    }
    
    // Verify the HMAC signature.
    // You'll need to get the "X-Signature" custom HTTP header.
    string hmacSignature = request.Headers.get("X-Signature");
    bool isValid = localResponse.IsValidHmacSignature(
         "obviously-fake-secret-key", hmacSignature);
    if (!isValid)
        throw new Exception("Bad HMAC signature! Is someone trying to do evil?");
    // Deserialize the response into objects
    var response = localResponse.DeserializeResponse<InferenceResponse>();
    
    // Print a summary of the parsed data
    System.Console.WriteLine(response.Inference);
}The Inference Object
This is the top-level object in the response.
It contains the following attributes:
- idUUID of the inference
- modelModel used for the inference
- fileMetadata concerning the file used for the inference
- resultResult of inference processing, the most important portion of the response.- Fields: For handling the extracted fields, see the Process Result Fields section. 
- Raw Text: For using the extracted text, see the Raw Text section. 
 
File Metadata
You can access various metadata concerning the file sent for processing.
Using the response deserialized object from either the polling response or a webhook payload.
from mindee import InferenceResponse
from mindee.parsing.v2 import InferenceFile
def handle_response(response: InferenceResponse):
    inference_file: InferenceFile = response.inference.file
    # various attributes are available, such as:
    filename: str = inference_file.name
    page_count: int = file.page_count
    mime_type: str = file.mine_typeUsing the response deserialized object from either the polling response or a webhook payload.
handleResponse(response) {
  const file = response.inference.file;
  // various attributes are available, such as:
  const filename = file.name;
  const pageCount = file.pageCount;
  const mimeType = file.mimeType;
}Using the $response deserialized object from either the polling response or a webhook payload.
use Mindee\Parsing\V2\InferenceResponse;
public function handleResponse(InferenceResponse $response)
{
    $file = $response->inference->file;
    // various attributes are available, such as:
    $filename = $file->name;
    $pageCount = $file->pageCount;
    $mimeType = $file->mimeType;
}Using the response deserialized object from either the polling response or a webhook payload.
require 'mindee'
def handle_response(response)
  file = response.inference.file
  # various attributes are available, such as:
  filename = inference_file.name
  page_count = file.page_count
  mime_type = file.mime_type
endUsing the response deserialized object from either the polling response or a webhook payload.
import com.mindee.parsing.v2.InferenceFile;
import com.mindee.parsing.v2.InferenceResponse;
public void handleResponse(InferenceResponse response) {
    InferenceFile file = response.inference.getFile();
    // various attributes are available, such as:
    String filename = file.getName();
    int pageCount = file.getPageCount();
    String mimeType = file.getMimeType();
}Using the response deserialized object from either the polling response or a webhook payload.
using Mindee.Parsing.V2;
public void HandleResponse(InferenceResponse response)
{
    InferenceFile file = response.Inference.File;
    // various attributes are available, such as:
    string filename = file.Name;
    int pageCount = file.PageCount;
    string mimeType = file.MimeType;
}Results
Fields
For handling the extracted fields, see the Process Result Fields section.
Raw Text
Access the full text content from the document, extracted as strings.
Using the response deserialized object from either the polling response or a webhook payload.
from mindee import InferenceResponse
def handle_response(response: InferenceResponse):
    raw_text = response.inference.result.raw_text
    
    # get the entire document as a single string
    document_text = str(raw_text)
    
    # loop over pages
    for page in raw_text.pages:
        page_text = page.contentUsing the response deserialized object from either the polling response or a webhook payload.
handleResponse(response) {
  const rawText = response.inference.result.rawText;
  // get the entire document as a single string
  const documentText = rawText.toString();
  
  // loop over pages
  for (const page of rawText.pages) {
    const pageText = page.content;
  }
}Using the $response deserialized object from either the polling response or a webhook payload.
use Mindee\Parsing\V2\InferenceResponse;
public function handleResponse(InferenceResponse $response)
{
    $rawText = $response->inference->result->rawText;
    // get the entire document as a single string
    $documentText = strval($rawText);
    // loop over pages
    foreach ($rawText->pages as $page) {
        $pageText = $page->content;
    }
}Using the response deserialized object from either the polling response or a webhook payload.
require 'mindee'
def handle_response(response)
  raw_text = response.inference.result.raw_text
  # get the entire document as a single string
  document_text = raw_text.to_s
  # loop over pages
  raw_text.pages.each do |page|
    page_text = page.content
  end
endUsing the response deserialized object from either the polling response or a webhook payload.
import com.mindee.parsing.v2.InferenceResponse;
import com.mindee.parsing.v2.RawText;
import com.mindee.parsing.v2.RawTextPage;
public void handleResponse(InferenceResponse response) {
    RawText rawText = response.getInference().getResult().getRawText();
    
    // get the entire document as a single string
    String documentText = rawText.toString();
    
    // loop over pages
    for (RawTextPage page : rawText.getPages()) {
        String pageText = page.getContent();
    }
}Using the response deserialized object from either the polling response or a webhook payload.
using Mindee.Parsing.V2;
public void HandleResponse(InferenceResponse response)
{
    RawText rawText = response.Inference.Result.RawText;
    
    // get the entire document as a single string
    string documentText = rawText.ToString();
    // loop over pages
    foreach (RawTextPage page in rawText.Pages)
    {
        string pageText = page.Content;
    }
}Last updated
Was this helpful?

