How to pass dynamic variables into application/json Bodies on UiPath HTTP Request activity?

What is the method for inserting dynamic variables into 'application/json' Bodies using the HTTP Request activity in UiPath?

Note: In this article, UiPath Studio 23.10.6 is used with a Windows project compatibility and UiPath.WebAPI.Activities.1.20.1 package from https://pkgs.dev.azure.com/uipath/Public.Feeds/_packaging/UiPath-Official/nuget/v3/index.json package source feed.

There may be instances where it may be needed to include dynamic variables in the HTTP Request Body payload for a VB.Net or C# project in UiPath Studio. However, there may be an uncertainty of how to accomplish this using the UiPath.WebAPI.Activities package.

As an example see the below JSON payload. The desire is to make it dynamic for some particular parameter values (the parameter values marked in red).

{
    "model": "XXXX-16k",
    "user_id": "john.doe@test.com",
    "messages": [{"role":"user","content": "This is a sample"}]}


For a VB.Net language project:

  1. Create two System.String variables user_id_value and content_value
  2. The body in the HTTP Request should look as below and make sure that the Body Format is set to be application/json.

"{
   ""model"": ""XXXX-16k"",
   ""user_id"": """+user_id_value+""",
   ""messages"": [{""role"":""user"",""content"": """+content_value+"""}]}"

Example:

image.png


For a C# language project:

  1. Create two System.String variables user_id_value and content_value
  2. The body in the HTTP Request should look as below and make sure that the Body Format is set to be application/json.
@"{" +
@"    ""model"": ""XXXX-16k""," +
@"    ""user_id"": """ + user_id_value + @"""," +
@"    ""messages"": [{""role"":""user"",""content"": """ + content_value + @"""}]" +
@"}"

Example: