Table of Contents

Saving via Webhook

Products: FastReport Cloud, FastReport Corporate Server, FastReport Publisher

In this article, we will consider the method of sending a report via webhook. Instructions on how to work with tasks are described in the General Information section.

Getting Started

You will need the following tools and capabilities:

  1. Knowledge of using API key in Service solutions.

    This article will skip additional information on authentication and authorization.

  2. .NET SDK.

  3. C# code editor or text editor, such as Visual Studio Code.

  4. Report template.

    It can be prepared using the free FastReport Community Designer program.

  5. Active subscription to one of the following products: FastReport Cloud, FastReport Corporate Server, or FastReport Publisher.

  6. Internet access.

  7. Client application that accepts requests.

Note! This guide assumes that you already know how to develop your application in the C# programming language.

Note! The points above describe the recommended tools.

Note! This guide assumes that you have a client application that accepts and processes incoming webhooks.

Creating a Task

Let's consider creating a template sending task via webhook and its subsequent execution:

// Object initialization
CreateWebhookTaskVM webhookTaskVM = new CreateWebhookTaskVM
{
    Name = "Webhook Sending Task",
    InputFile = new InputFileVM
    {
        EntityId = "{template identifier}",
        Type = FileKind.Template
    },
    Url = new Uri("https://example.com/"),
    Headers = new Dictionary<string, string> {
        { "Authorization", "Bearer <token>" },
        { "Content-Length", "1238" }
    },
    SubscriptionId = "{workspace identifier}"
};

 // Creating a task
 TaskBaseVM webhookTask = await tasksClient.CreateTaskAsync(webhookTaskVM);

 // Run task by identifier
 await tasksClient.RunTaskByIdAsync(webhookTask.Id);

Note! Real object identifiers should be entered in the EntityId and SubscriptionId fields. Otherwise, the task will be aborted with an error.

Note! EntityId can be passed template, report, and export identifier.

Executing Task from Request Body

// Run task from request body
 await tasksClient.RunTaskAsync(new RunWebhookTaskVM
 {
     InputFile = new RunInputFileVM
     {
         EntityId = "{template identifier}",
         Type = FileKind.Template
     },
     Url = new Uri("https://example.com/"),
     Headers = new Dictionary<string, string> {
         { "Authorization", "Bearer <token>" },
         { "Content-Type", "multipart/form-data" }
     },
     SubscriptionId = "{workspace identifier}"
 });

Note! In this case, the webhook sending will be performed directly by this request and the task will not be saved in the database.

Updating Task by Identifier

await tasksClient.UpdateTaskAsync("{old task identifier}", new UpdateWebhookTaskVM()
 {
     InputFile = new RunInputFileVM
     {
         EntityId = "{updated template identifier}",
         Type = FileKind.Template
     },
     Url = new Uri("{Updated address}"),
     Headers = new Dictionary<string, string> {
         { "{Updated header}", "{Updated value}" },
     }
 });

Example of a request that will come to the webhook endpoint:

{
  "startedDateTime": "2024-06-07 08:37:09",
  "request": {
    "method": "POST",
    "url": "https://example.com/6e259560-25e5-482b-a7b2-8c5267ba6ae3/",
    "headers": [
      {
        "name": "connection",
        "value": "close"
      },
      {
        "name": "content-length",
        "value": "1421"
      },
      {
        "name": "content-type",
        "value": "multipart/form-data; boundary=\"62ec6524-9360-4e91-834f-e9531e2d2c30\""
      },
      {
        "name": "host",
        "value": "example.com"
      }
    ],
    "bodySize": 0,
    "postData": {
      "mimeType": "application/json",
      "text": ""
    }
  },
  "response": {
    "status": 200,
    "httpVersion": "HTTP/1.1",
    "headers": [
      {
        "name": "Content-Type",
        "value": "text/html"
      }
    ],
    "content": {
      "size": 145,
      "text": "This URL has no default content configured.",
      "mimeType": "text/html"
    }
  }
}