Here's a .NET 4.6.2 API web client uploading a file - simple stuff:
var webClient = new ApiWebClient();
webClient.Timeout = timeout;
webClient.Headers.Add( HttpHeaderNames.CONTENT_TYPE, contentType );
webClient.Headers.Add( HttpHeaderNames.CONTENT_DISPOSITION, disp );
foreach (var h in _headers)
webClient.Headers.Add(h.Key, h.Value);
respBytes = webClient.UploadFile(FullUrl, fileFullPath);
My problem is trying to upload a file in .NET 8 with HttpClient.
Unfortunately I don't have access to the server receiving the request, but it sends back a response that looks like no file was attached. The current iteration of my code is something like this:
var httpClient = GetHttpClient(); // gets from the HttpClientFactory
httpClient.Timeout = new TimeSpan( 0, 0, timeout );
var uriBuilder = BuildUriWithGets();
var req = new HttpRequestMessage( HttpVerb, uriBuilder.ToString() );
req.Headers.TryAddWithoutValidation( HttpHeaderNames.CONTENT_TYPE, contentType );
req.Headers.TryAddWithoutValidation( HttpHeaderNames.CONTENT_DISPOSITION, disp );
foreach (var h in _headers)
request.Headers.TryAddWithoutValidation(h.Key, h.Value);
HttpResponseMessage resp;
using var requestContent = new MultipartFormDataContent();
using var fileStream = File.OpenRead(fileFullPath);
requestContent.Add(new StreamContent(fileStream));
req.Content = requestContent;
resp = httpClient.Send(req);
Is this the proper way to send a file with headers and query params? I want the file to be the "file" parameter (I've tried iterations on this to no avail).
Moreover, is there a good way to see the raw message being sent via some text dump (I'm running this via Visual Studio). I could compare the ApiWebClient request and the HttpClient request to see what's missing.