0

I've tried everything but cannot seem to get a Variable into some text. I think the problem is that the test is in JSON format. I'm trying to pass a variable from a textbox in the UI to this variable, but for testing I've just created a local variable, as below. Here is my code:

Variable:

string Repo1 = "jamesbennett12345678990";

String I'm trying to add my variable into:

var UserAgent1 = new ProductInfoHeaderValue("ScraperBot", "1.0");
request.Headers.TryAddWithoutValidation("Authorization", "token testtesttesttest");
request.Headers.UserAgent.Add(UserAgent1);

//String here
request.Content = new StringContent("{\"name\":\"{Repo1}\"}");

I've read that to do this it is simply a case of putting curly braces around the variable but it doesnt work. I know this because the line - string Repo1 = "jamesbennett12345678990"; says that the variable is not in use.

This part is JSON Data that is getting passed using the HTPClient so I'm thinking as there are already Speachmarks escaped with backslashes that there might be a formatting problem here ---- ("{"name":"{Repo1"}")

I don't have any errors as such, only the one in VS saying the

var string Repo1 = "jamesbennett12345678990";

isn't actually in use.

I've also looked into string Interpolation.

10
  • 4
    You're confusing C# Interpolated strings $"foo {bar} baz" with JSON syntax. Commented Jan 6, 2022 at 16:41
  • you simply need to add the dollar sign before the text: new StringContent($"\{\"name\":\"{Repo1}\"\}"); and escape the curly braces that are not representing a variable. Commented Jan 6, 2022 at 16:43
  • 1
    If you want to form your json manually, consider new StringContent($@"{{""name"":""{Repo1}""}}");. If you want to get a serializer to form it, JsonConvert.SerializeObject(new{name=Repo1}); (Newtonsoft) or JsonSerializer.Serialize(new { name = Repo1 }); (STJ) - personally I'd go the latter route of "use a serializer lib". You've bought a dog, don't bark yourself Commented Jan 6, 2022 at 16:43
  • 2
    i would consider using a json library to ensure its encoded properly. Commented Jan 6, 2022 at 16:44
  • 2
    As a side note, building json manually is discouraged. It may work in simple cases, but it can quickly became a headache with all required escaping. It's also a potential bug source, because the string you inject can't contains any json language character (if it does, it will break your json or worse, be a attack vector). Use Json library to read and write json. Commented Jan 6, 2022 at 16:47

1 Answer 1

3

try this

var json = System.Text.Json.JsonSerializer.Serialize(new {name=Repo1});
//or
var json = System.Text.Json.JsonSerializer.SerializeToElement(new {name=Repo1}).ToString();
//or 
var json="{\"name\":\""+ Repo1+"\"}";

var content = new StringContent(json, Encoding.UTF8, "application/json");
Sign up to request clarification or add additional context in comments.

2 Comments

For single values, it's likely more efficient to use JsonElement or JsonNode (which are rough equivalents of JToken in System.Text.Json) instead of passing a C# anonymous-type.
@Dai Thank you. Maybe you are right. I can create a string manually as well. But I don' t see much difference for one line of the code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.