Search code examples
c#asp.net-web-apihttpclient

C# Web API - POST with ID only


I have an endpoint that clones a record but to do this I only need to use an Id. Since I only need to use an Id I've written this endpoint:

[HttpPost]
[Route("test/clone/{id}")]
[SwaggerResponse(HttpStatusCode.OK, description: "Clone an existing record", type: typeof(int))]
public async Task<IHttpActionResult> Clone(int id)
{
    var newId = _service.DoSomething(id);
    return Ok(newId);
}

I then want to call this endpoint in a test but I don't know how to write this:

 var result =
            await WebServer.HttpClient.PostAsync($"test/clone/{id}", // WHAT GOES HERE??) 

  var newId = await result.Content.ReadAsAsync<int>();

Does anyone know how I can call this endpoint using HttpClient??


Solution

  • You can just pass null and it will send it with no body e.g.

     var result =
                await WebServer.HttpClient.PostAsync($"test/clone/{id}", null)