skip to Main Content

I have a REST endpoint to which I need to pass the input parameter like below within the quotes

/api/rooms?filter=name='A1'

If I donot pass the parameter with quotes it will error. So within my code how can I call the end point with the input parameter with in single quotes and get the response. Below is what I tried but not sure it is the correct way

public async Task<string> GetRoomID(string roomName)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(_iconfiguration.GetSection("RoomMovement").GetSection("BaseAddress").Value);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.GetAsync("api/rooms?filter=name=" + roomName).ConfigureAwait(false);
                if (response.IsSuccessStatusCode)
                {
                string result = response.Content.ReadAsStringAsync().Result;

2

Answers


  1. You can try this:

    HttpResponseMessage response = await client.GetAsync($"api/rooms?filter=name='{roomName}'").ConfigureAwait(false);
    
    Login or Signup to reply.
  2. Have you tried this? You can just add the ‘s to your string individually!

    HttpResponseMessage response = await client.GetAsync("api/rooms?filter=name=" + "'" + roomName + "'").ConfigureAwait(false);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search