Search code examples
c#.netfacebook-graph-api

C# How to put curly brackets in URL


I've been trying to get the data that I need from Facebook's graph api explorer but unfortunately, cant pass the fields necessary in URL via C#.

Here's what I've tried so far:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://graph.facebook.com");

    HttpResponseMessage response = client.GetAsync($"cocacola?fields=posts{id,created_time,permalink_url,message,link,type,full_picture}&access_token={textBox1.Text}").Result;
    response.EnsureSuccessStatusCode();

    string result = response.Content.ReadAsStringAsync().Result;

    var jsonRes = JsonConvert.DeserializeObject<dynamic>(result);

    var returned = jsonRes.ToString();

    MessageBox.Show(returned);
}

What's needed to be done here for fetching data via API? :)


Solution

  • As it turns out, there is an issue because you're using string interpolation and also want curly braces in your string. You'll have to escape them by doubling them.

    HttpResponseMessage response = client.GetAsync($"cocacola?fields=posts{{id,created_time,permalink_url,message,link,type,full_picture}}&access_token={textBox1.Text}").Result;