Home > Software engineering >  Adding a variable to the verbatim string literal
Adding a variable to the verbatim string literal

Time:08-26

I am new to C# programming.

I am trying to automate some API tests and passing payload in a POST request using RestSharp library. That payload looks like this

public class Payload
    {
        public string firstPayload = @"{
"   "\n"  
@"  ""channel"": ""XYZ"",
"   "\n"  
@"  ""Email"": ""[email protected]""
"   "\n"  
@"}";
}

I call this payload in the Test Project. This request will look something like this in Postman (JSON)

{
  "channel": "XYZ",
  "Email": "[email protected]"
}

But since I can't use the same email over and over, now I want to add (DateTime.Now) in YYYYMMDDTHHMMSS format with the email, so that everytime I run my test, it's a unique email. The email should look like "[email protected]"

But I am not sure how to add current date time within the verbatim string firstPayload C# code

CodePudding user response:

You can do something like this

var payload = $@"
{{
    ""channel"": ""XYZ"",
    ""Email"": ""abc{DateTime.Now}@abc.com""
}}";

You don't need to manually add "/n" when using string literal

  • Related