user4912134
user4912134

Reputation: 1043

Pass Input parameter to the GET Rest API call

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;

Upvotes: 0

Views: 1344

Answers (2)

Brandon Noel
Brandon Noel

Reputation: 66

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);

Upvotes: 1

Daniel
Daniel

Reputation: 9829

You can try this:

HttpResponseMessage response = await client.GetAsync($"api/rooms?filter=name='{roomName}'").ConfigureAwait(false);

Upvotes: 3

Related Questions