Question : Do we have provision to read the Response data from a file? #86

Closed
opened 2025-12-29 08:22:01 +01:00 by adam · 4 comments
Owner

Originally created by @raghavendrabankapur on GitHub (Apr 2, 2018).

@StefH We have a huge data that need to be responded with and also this is been used by other mocks. Can we respond to a matching request with a file content?

Originally created by @raghavendrabankapur on GitHub (Apr 2, 2018). @StefH We have a huge data that need to be responded with and also this is been used by other mocks. Can we respond to a matching request with a file content?
adam added the question label 2025-12-29 08:22:01 +01:00
adam closed this issue 2025-12-29 08:22:02 +01:00
Author
Owner

@StefH commented on GitHub (Apr 5, 2018):

Hello @raghavendrabankapur

This is not yet described in the Wiki (maybe I need to restructure the wiki), but you can use

BodyAsFile in the JSON mapping
or
in C# code:

/// <summary>
/// WithBodyFromFile : Create a ... response based on a File.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="cache">Defines if this file is cached in memory or retrieved from disk everytime the response is created.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithBodyFromFile([NotNull] string filename, bool cache = true);
@StefH commented on GitHub (Apr 5, 2018): Hello @raghavendrabankapur This is not yet described in the Wiki (maybe I need to restructure the wiki), but you can use `BodyAsFile` in the JSON mapping or in C# code: ``` c# /// <summary> /// WithBodyFromFile : Create a ... response based on a File. /// </summary> /// <param name="filename">The filename.</param> /// <param name="cache">Defines if this file is cached in memory or retrieved from disk everytime the response is created.</param> /// <returns>A <see cref="IResponseBuilder"/>.</returns> IResponseBuilder WithBodyFromFile([NotNull] string filename, bool cache = true); ```
Author
Owner

@raghavendrabankapur commented on GitHub (Apr 5, 2018):

@StefH in that case, the file name should be the full path of the file , or file should be placed somewhere the similar way of __admin/mappings.

@raghavendrabankapur commented on GitHub (Apr 5, 2018): @StefH in that case, the file name should be the full path of the file , or file should be placed somewhere the similar way of __admin/mappings.
Author
Owner

@StefH commented on GitHub (Apr 5, 2018):

As of now, you need to provide the full path. Maybe it's a good idea that the code can support both.

/// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromFile"/>
public IResponseBuilder WithBodyFromFile(string filename, bool cache = true)
{
	Check.NotNull(filename, nameof(filename));

	ResponseMessage.BodyEncoding = null;
	ResponseMessage.BodyAsFileIsCached = cache;

	if (cache)
	{
		ResponseMessage.Body = null;
		ResponseMessage.BodyAsBytes = File.ReadAllBytes(filename);
		ResponseMessage.BodyAsFile = null;
	}
	else
	{
		ResponseMessage.Body = null;
		ResponseMessage.BodyAsBytes = null;
		ResponseMessage.BodyAsFile = filename;
	}

	return this;
}
@StefH commented on GitHub (Apr 5, 2018): As of now, you need to provide the full path. Maybe it's a good idea that the code can support both. ``` c# /// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromFile"/> public IResponseBuilder WithBodyFromFile(string filename, bool cache = true) { Check.NotNull(filename, nameof(filename)); ResponseMessage.BodyEncoding = null; ResponseMessage.BodyAsFileIsCached = cache; if (cache) { ResponseMessage.Body = null; ResponseMessage.BodyAsBytes = File.ReadAllBytes(filename); ResponseMessage.BodyAsFile = null; } else { ResponseMessage.Body = null; ResponseMessage.BodyAsBytes = null; ResponseMessage.BodyAsFile = filename; } return this; } ```
Author
Owner

@StefH commented on GitHub (Sep 11, 2018):

Also this linked issue could help you ?
https://github.com/WireMock-Net/WireMock.Net/issues/173

Closing this issue for now. If you still have question, please reopen or create a new issue.

@StefH commented on GitHub (Sep 11, 2018): Also this linked issue could help you ? https://github.com/WireMock-Net/WireMock.Net/issues/173 Closing this issue for now. If you still have question, please reopen or create a new issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#86