Wiremock.net (C#) how to get a PDF from a Request body (form-data ) #575

Closed
opened 2025-12-29 08:30:25 +01:00 by adam · 2 comments
Owner

Originally created by @eetawil on GitHub (Feb 14, 2024).

I tired on SO: https://stackoverflow.com/questions/77989392/wiremock-net-c-how-to-get-a-pdf-from-a-request-body-form-data

I'm also posting it here to get better reach...

=>
We are using wiremock.net (C#) and we want to extract from the request the PDF file contained in that Request. How can we do that? I'm new to wiremock and I read about response templating without success.

Can someone please help with either documentation or code sample for this?

In other words how to GET_PDF FILE_HERE_FROM_REQUEST (see code below)

image

wireMockServer
            .Given(Request.Create().WithPath("/validatePDFA").UsingPost())
            .RespondWith(Response.Create()
                .WithStatusCode(200)
                .WithHeader("Content-Type", "form-data")
                .WithBody(req => ReadFileAndGenerateJsonBody(GET_PDF FILE_HERE_FROM_REQUEST))                    
            );

 public string ReadFileAndGenerateJsonBody(...)
{
     //read text from PDF  file 
     //build jsonResponse
     return jsonResponse
}
Originally created by @eetawil on GitHub (Feb 14, 2024). I tired on SO: https://stackoverflow.com/questions/77989392/wiremock-net-c-how-to-get-a-pdf-from-a-request-body-form-data I'm also posting it here to get better reach... => We are using wiremock.net (C#) and we want to extract from the request the PDF file contained in that Request. How can we do that? I'm new to wiremock and I read about response templating without success. Can someone please help with either documentation or code sample for this? In other words how to GET_PDF FILE_HERE_FROM_REQUEST (see code below) ![image](https://github.com/WireMock-Net/WireMock.Net/assets/12733745/2f6f3c14-8823-4c75-8a87-f2ab8d733333) ``` c# wireMockServer .Given(Request.Create().WithPath("/validatePDFA").UsingPost()) .RespondWith(Response.Create() .WithStatusCode(200) .WithHeader("Content-Type", "form-data") .WithBody(req => ReadFileAndGenerateJsonBody(GET_PDF FILE_HERE_FROM_REQUEST)) ); public string ReadFileAndGenerateJsonBody(...) { //read text from PDF file //build jsonResponse return jsonResponse } ```
adam added the question label 2025-12-29 08:30:25 +01:00
adam closed this issue 2025-12-29 08:30:25 +01:00
Author
Owner

@StefH commented on GitHub (Feb 14, 2024):

In your example you are posting a form which has 2 fields: a file field, which contains the PDF and a json text field.

If you want to return a JSON response, you need to use the method (which gives you the request object and should return an anonymous object which will be serialized to JSON:

IResponseBuilder WithBodyAsJson(Func<IRequestMessage, Task<object>> bodyFactory, Encoding? encoding = null);

And the IRequestMessage contains a property BodyAsMimeMessage which is an MimeKit.MimeMessage object which contains several body parts which you can get and use if needed.
Example:

foreach (var mimePart in message.BodyParts.OfType<MimeKit.MimePart>())
{
// ...
}

See https://github.com/jstedfast/MimeKit for more details

@StefH commented on GitHub (Feb 14, 2024): In your example you are posting a form which has 2 fields: a file field, which contains the PDF and a json text field. If you want to return a JSON response, you need to use the method (which gives you the request object and should return an anonymous object which will be serialized to JSON: ``` c# IResponseBuilder WithBodyAsJson(Func<IRequestMessage, Task<object>> bodyFactory, Encoding? encoding = null); ``` And the `IRequestMessage` contains a property `BodyAsMimeMessage` which is an `MimeKit.MimeMessage` object which contains several body parts which you can get and use if needed. Example: ``` c# foreach (var mimePart in message.BodyParts.OfType<MimeKit.MimePart>()) { // ... } ``` See https://github.com/jstedfast/MimeKit for more details
Author
Owner

@eetawil commented on GitHub (Feb 14, 2024):

Thank you very much for your input and directions!

@eetawil commented on GitHub (Feb 14, 2024): Thank you very much for your input and directions!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#575