Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug option that enables writing the raw request body to file system for debugging #582

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/TesApi.Web/Controllers/TaskServiceApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -127,6 +129,8 @@ public virtual async Task<IActionResult> CancelTask([FromRoute][Required] string
[SwaggerResponse(statusCode: 200, type: typeof(TesCreateTaskResponse), description: "")]
public virtual async Task<IActionResult> CreateTaskAsync([FromBody] TesTask tesTask, CancellationToken cancellationToken)
{
await WriteRawRequestBodyToLocalFileAsync(cancellationToken);

if (!string.IsNullOrWhiteSpace(tesTask.Id))
{
return BadRequest("Id should not be included by the client in the request; the server is responsible for generating a unique Id.");
Expand Down Expand Up @@ -233,6 +237,19 @@ public virtual async Task<IActionResult> CreateTaskAsync([FromBody] TesTask tesT
return StatusCode(200, new TesCreateTaskResponse { Id = tesTask.Id });
}

private async Task WriteRawRequestBodyToLocalFileAsync(CancellationToken cancellationToken)
{
// Enable the request stream to be read multiple times
Request.EnableBuffering();
Request.Body.Position = 0; // Reset the position to the start of the stream
using var reader = new StreamReader(Request.Body);
var requestBody = await reader.ReadToEndAsync();
// Reset the stream position to read it again later if needed
Request.Body.Position = 0;
string filePath = Path.Combine(Path.GetTempPath(), $"{DateTime.UtcNow:yyyyMMdd_HHmmss}.json");
await System.IO.File.WriteAllTextAsync(filePath, requestBody, cancellationToken);
}

/// <summary>
/// GetServiceInfo provides information about the service, such as storage details, resource availability, and other documentation.
/// </summary>
Expand Down
Loading