How to append request body properties for response templating? #565

Closed
opened 2025-12-29 15:27:10 +01:00 by adam · 5 comments
Owner

Originally created by @felipetofoli on GitHub (Jan 10, 2024).

Hi @StefH ! I hope this message finds you well! 😃

Question

-> How to append request body properties for response templating?

Context

I am trying to append two values received in the request, but I receive the following error when trying to append them:
500 Internal server error

{
    "Status": "Object of type 'Newtonsoft.Json.Linq.JValue' cannot be converted to type 'System.String'."
}

Mapping

server
    .Given(Request
        .Create()
        .UsingPost())
    .WithData(new 
    {
        data = new Dictionary<string, string>
        {
            { "1234567890SEDCRFVGTBHYH", "value" }
        }
    })
    .RespondWith(Response
        .Create()
        .WithStatusCode(200)
        .WithHeader("Content-Type", "application/json")
        .WithBodyAsJson(new
        {
            testAppend = "{{ String.Append request.bodyAsJson.first_id request.bodyAsJson.second_id }}",            
            valueGotFromDict = "{{ String.FormatAsString (lookup data (String.Append request.bodyAsJson.first_id request.bodyAsJson.second_id) '') }}",
        })
        .WithTransformer());

My objective is to be able to get the value according to the valueGotFromDict response property (concatenate 2 request body properties to generate the key that I will search for in a dictionary, but I am not able to resolve the "Append" part at the moment.

Request example

{
    "first_id" : "1234567890",
    "second_id":"SEDCRFVGTBHYH"
}

I wasn't able to find examples in the docs or the internet, so could you please help me?

Please let me know if you need any information.
Thanks!

Originally created by @felipetofoli on GitHub (Jan 10, 2024). Hi @StefH ! I hope this message finds you well! 😃 ### Question -> **How to append request body properties for response templating?** ### Context I am trying to append two values received in the request, but I receive the following error when trying to append them: **500 Internal server error** ``` { "Status": "Object of type 'Newtonsoft.Json.Linq.JValue' cannot be converted to type 'System.String'." } ``` ### Mapping ```csharp server .Given(Request .Create() .UsingPost()) .WithData(new { data = new Dictionary<string, string> { { "1234567890SEDCRFVGTBHYH", "value" } } }) .RespondWith(Response .Create() .WithStatusCode(200) .WithHeader("Content-Type", "application/json") .WithBodyAsJson(new { testAppend = "{{ String.Append request.bodyAsJson.first_id request.bodyAsJson.second_id }}", valueGotFromDict = "{{ String.FormatAsString (lookup data (String.Append request.bodyAsJson.first_id request.bodyAsJson.second_id) '') }}", }) .WithTransformer()); ``` My objective is to be able to get the value according to the `valueGotFromDict` response property (concatenate 2 request body properties to generate the key that I will search for in a dictionary, but I am not able to resolve the "Append" part at the moment. ### Request example ```json { "first_id" : "1234567890", "second_id":"SEDCRFVGTBHYH" } ``` --- I wasn't able to find examples in the docs or the internet, so could you please help me? Please let me know if you need any information. Thanks!
adam added the question label 2025-12-29 15:27:10 +01:00
adam closed this issue 2025-12-29 15:27:10 +01:00
Author
Owner

@StefH commented on GitHub (Jan 10, 2024):

Maybe, you can try:

{{Expression request.bodyAsJson.first_id.ToString() + request.bodyAsJson.second_id.ToString() }}
@StefH commented on GitHub (Jan 10, 2024): Maybe, you can try: ``` handlebars {{Expression request.bodyAsJson.first_id.ToString() + request.bodyAsJson.second_id.ToString() }} ```
Author
Owner

@StefH commented on GitHub (Jan 25, 2024):

@felipetofoli
Did you try my suggestion?

@StefH commented on GitHub (Jan 25, 2024): @felipetofoli Did you try my suggestion?
Author
Owner

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

@felipetofoli
Did you have time to try my suggestion?

@StefH commented on GitHub (Feb 13, 2024): @felipetofoli Did you have time to try my suggestion?
Author
Owner

@felipetofoli commented on GitHub (Feb 16, 2024):

Hello @StefH !
I am sorry for this late reply. I didn't see the notification for this thread.

It didn't work. I had to write a custom handlebar to achieve my intentions, unfortunately.

handlebarsContext.RegisterHelper("Concat", (output, context, arguments) =>
{
    var stringBuilder = new StringBuilder();

    foreach (var argument in arguments)
        stringBuilder.Append(GetArgumentValue(argument));

    output.WriteSafeString(stringBuilder.ToString());
});

I started debugging the project (weeks ago), and if I recall correctly, it seemed that the context was not loaded when executing the Append handlebar function... Unfortunately, it was weeks ago, and I don't have (or remember) more details to provide... 😢

@felipetofoli commented on GitHub (Feb 16, 2024): Hello @StefH ! I am sorry for this late reply. I didn't see the notification for this thread. It didn't work. I had to write a custom handlebar to achieve my intentions, unfortunately. ```csharp handlebarsContext.RegisterHelper("Concat", (output, context, arguments) => { var stringBuilder = new StringBuilder(); foreach (var argument in arguments) stringBuilder.Append(GetArgumentValue(argument)); output.WriteSafeString(stringBuilder.ToString()); }); ``` I started debugging the project (weeks ago), and if I recall correctly, it seemed that the context was not loaded when executing the Append handlebar function... Unfortunately, it was weeks ago, and I don't have (or remember) more details to provide... 😢
Author
Owner

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

OK. If you got it working thats good.

Append and Concat are included. See:
https://github.com/Handlebars-Net/Handlebars.Net.Helpers/wiki/String#append

@StefH commented on GitHub (Feb 16, 2024): OK. If you got it working thats good. --- Append and Concat are included. See: https://github.com/Handlebars-Net/Handlebars.Net.Helpers/wiki/String#append
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#565