0

Below is the method, where I have an HttpClient method defined with Post method, the webservice url is called from a constructor. I would like to pass a query parameter in the URL along with body, I have the body defined here but not sure the best way to pass the query parameter. If I have web service url as :

http://localhost:7071/api/Function1 

I would like my overwrite argument to be passed as a parameter like this:

http://localhost:7071/api/Function1?read = True/false(value of overwrite)

Code:

private async Task UpsertInt<T>(T rec, bool overwrite) where T : Document
        {
            HttpClient client = new HttpClient();
            var ser = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore };
            var wr = new StringWriter();
            ser.Serialize(wr, rec);
            await client.PostAsJsonAsync(WebService, wr.ToString());                         
            
        }
Ankit Kumar
  • 476
  • 1
  • 11
  • 38

1 Answers1

1

You could try like this:

private async Task UpsertInt<T>(T rec, bool overwrite) where T : Document
        {
            HttpClient client = new HttpClient();
            var ser = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore };
            var wr = new StringWriter();
            ser.Serialize(wr, rec);
            await client.PostAsJsonAsync(WebService + "?read=" + overwrite, wr.ToString());                         
            
        }

To make it safer, you should use a function converting an object to query parameters that are url encoded like this: Convert querystring from/to object

The PostAsync itself, does not support query parameters and for a good reason. I would expect the metadata like overwrite, to pass through the wire as a custom header.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61