Skip to content

[C#] Api Client avec une réponse autre que JSON #562

@etienne582

Description

@etienne582

L'api client veut obligatoirement un json en retour.
Domaine :

domain:
  name: DO_ACTION_RESULT
  mediaType: multipart/form-data
  label: Action result
  ts:
    type: Response
  csharp:
    type: IActionResult
    imports:
      - Microsoft.AspNetCore.Mvc

API :

module: PdfGenerator
tags:
  - api-client
---
endpoint:
  name: GeneratePdf
  method: POST
  route: api/pdf/{templateName}
  description: Generate a PDF based on the provided template name and data.
  params:
    - name: templateName
      domain: DO_TEXT
      required: true
      comment: The name of the PDF template.
      
    - name: data
      domain: DO_MAP
      required: true
      comment: Key/value data used by the template.

    - name: withBackground
      domain: DO_BOOLEAN
      required: true
      comment: Should the document include a background.
  returns:
    name: PdfDocument
    domain: DO_ACTION_RESULT
    required: true
    comment: The generated PDF document as a stream.

Code généré :

public async Task<IActionResult> GeneratePdf(string templateName, IDictionary<string, string> data, bool? withBackground = null, CancellationToken ct = default)
{
    await EnsureAuthentication(ct);
    var query = await new FormUrlEncodedContent(new Dictionary<string, string?>
    {
        ["withBackground"] = withBackground?.ToString(),
    }.Where(kv => kv.Value != null)).ReadAsStringAsync(ct);
    using var res = await client.SendAsync(new(HttpMethod.Post, $"api/pdf/{templateName}?{query}") { Content = JsonContent.Create(data, options: _jsOptions) }, HttpCompletionOption.ResponseHeadersRead, ct);
    await EnsureSuccess(res, ct);

    return (await res.Content.ReadFromJsonAsync<IActionResult>(_jsOptions, ct))!;
}

Il faudrait certainement un ReadFromStreamAsync

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions