Getting Some Randam ID when Sending a file using MultipartContent() and HttpClient - c#

I am trying to send a file via an API. I was able to receive the file in my API, but for some reason I am seeing random ID(could be content id) when I read the content in my API.
Previously I was using seeing info about content type and file info as well. I was using MultipartFormDataContent() at that time, then I switched to MultipartContent(). Now the content type and file info is gone, but I still see the id at the start and end of the content.
Code that I am using to send file
using (var formContent = new MultipartContent())
{
byte[] fileByteArray;
using (var binaryReader = new BinaryReader(file.OpenReadStream()))
{
fileByteArray = binaryReader.ReadBytes((int)file.Length);
}
var content = new ByteArrayContent(fileByteArray, 0, fileByteArray.Length);
formContent.Add(content);
// Add the file and it's content
formContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(file.ContentType);
formContent.Headers.ContentType.CharSet = string.Empty;
formContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
HttpClient httpClient = new HttpClient();
var task = Task.Run(() => httpClient.PostAsync("MY_END_POINT", formContent));
task.Wait();
HttpResponseMessage response = task.Result;
response.EnsureSuccessStatusCode();
}
Code to read file
using (StreamReader reader = new StreamReader(fileUpload.FileContent))
{
string content = reader.ReadToEnd();
}
Any idea on how the id can be removed?

Related

Null response when using RestSharp to download a file

I am attempting to update the RestSharp file download portion code in one of my applications. Apparently the .SaveAs() is being depricated, so I'm trying to follow their updated example for working with files. However, my response is always null, and the temp file that is created doesn't seem to be filled with the data I'm attempting to save.
Here's what I have so far:
var tempFile = Path.GetTempFileName();
using var writer = File.OpenWrite(tempFile);
var client = new RestClient("https://provider-api.spotify.com/v1/analytics");
var request = new RestRequest("{licensor}/enhanced/tracks/{year}/{month}/{day}", Method.GET);
request.AddHeader("Authorization", $#"Bearer {token}");
request.AddUrlSegment("licensor", "licensor_name");
request.AddUrlSegment("year", 2021);
request.AddUrlSegment("month", 1);
request.AddUrlSegment("day", 10);
var checkResponse = client.Execute<SpotifyTracksResourceModel>(request);
if (checkResponse.Content == "")
{
Console.WriteLine("No data");
}
request.ResponseWriter = responseStream =>
{
using (responseStream)
{
responseStream.CopyTo(writer);
}
};
var response = client.DownloadData(request);
I threw in the checkResponse code to ensure that I am actually getting data back, and I am in fact getting data. But as I said, once it gets to the var response = ... line, it comes back NULL, and nothing has been written to that temp file.
Thank you in advance for any help with this!
So it ended up being a combination of a few little things I needed to tweak. But the biggest things were updating the RestSharp NuGet package, and closing off the writer FileStream.
var tempFile = Path.GetTempFileName();
using var writer = File.OpenWrite(tempFile);
var client = new RestClient("https://provider-api.spotify.com/v1/analytics");
var request = new RestRequest("{licensor}/enhanced/tracks/{year}/{month}/{day}", DataFormat.Json)
.AddUrlSegment("licensor", "licensor_name")
.AddUrlSegment("year", "2021")
.AddUrlSegment("month", "1")
.AddUrlSegment("day", "10");
spotifyRequest.AddHeader("Authorization", $#"Bearer {token}");
var checkResponse = spotifyClient.Get<SpotifyTracksResourceModel>(spotifyRequest);
request.ResponseWriter = responseStream =>
{
using (responseStream)
{
responseStream.CopyTo(writer);
}
};
var response = client.DownloadData(request);
writer.Close();

Download file using HttpClient while also sending POST data

I am trying to send a json POST request and save the file returned. This is my code:
var obj = new {data = "zip", name = "h.zip"};
string objString = JsonConvert.SerializeObject(obj);
StringContent stringContent = new StringContent(objString, Encoding.UTF8, "application/json");
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Post, downloadUrl))
{
var result = await httpClient.PostAsync(downloadUrl, stringContent);
Console.WriteLine(result.StatusCode);
var contentStream = await result.Content.ReadAsStreamAsync();
Console.WriteLine(contentStream); // debugger confirms that there is a file present
using (FileStream stream = new FileStream(#"C:\Users\mainuser\Desktop\downloadtest\file.zip", FileMode.Create, FileAccess.Write, FileShare.None, Int32.MaxValue)){
Console.WriteLine("copying started");
await contentStream.CopyToAsync(stream);
Console.WriteLine("never gets printed");
}
}
}
It successfully creates a file.zip, but it maintains to be 0KB in size. Also, Console.WriteLine("never gets printed"); never executes. What is the correct way of downloading file using HttpClient?

Document uploaded to MS Teams using graph API gets corrupted

I am trying to upload a document to Microsoft Teams using Microsoft Graph (beta version), but the document gets corrupted after a successful upload.
Using Graph, I'm first creating an Group, creating a Team based on the Group, adding some Team Members and finally uploading a document to the default channel.
All works fine except the uploaded document gets corrupted and the Office Online editor is not able to open it. We can however download the file and open in Microsoft Word after correcting the file.
Below is the code that I'm using for document upload->
FileInfo fileInfo =
new FileInfo(#"F:\Projects\TestProjects\MSTeamsSample\MSTeamsSample\Files\Test File.docx");
var bytes = System.IO.File.ReadAllBytes(fileInfo.FullName);
var endpoint = $"https://graph.microsoft.com/beta/groups/{groupId}/drive/items/root:/General/{fileInfo.Name}:/content";
var fileContent = new ByteArrayContent(bytes);
fileContent.Headers.ContentType =
MediaTypeHeaderValue.Parse("application/octet-stream");
var requestContent = new MultipartFormDataContent();
requestContent.Add(fileContent, "File", fileInfo.Name);
var request = new HttpRequestMessage(HttpMethod.Put, endpoint);
request.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", "<Access Token>");
request.Content = requestContent;
var client = new HttpClient();
var response = client.SendAsync(request).Result;
I tried changing content type to application/vnd.openxmlformats-officedocument.wordprocessingml.document but no luck. I don't understand what could be wrong here. The code is pretty straight forward, based on the this documentation. Any help will be highly appreciated.
Please try this:
var filePath = #"F:\Projects\TestProjects\MSTeamsSample\MSTeamsSample\Files\Test File.docx";
var fileName = Path.GetFileName(filePath);
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
var endpoint = $"https://graph.microsoft.com/beta/groups/{groupId}/drive/items/root:/General/{fileName}:/content";
using (var client = new HttpClient())
{
using (var content = new StreamContent(fileStream))
{
content.Headers.Add("Content-Type", MimeMapping.GetMimeMapping(fileName));
// Construct the PUT message towards the webservice
using (var request = new HttpRequestMessage(HttpMethod.Put, endpoint))
{
request.Content = content;
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokenResponse.Token);
// Request the response from the webservice
using (var response = await client.SendAsync(request))
{
// Check the response.
}
}
}
}
I am able to see Word document in Microsoft Teams editor.

.NET multipart data appeared in preamble section

client side is using MultipartFormDataContent with two part of data, one is a file, the other one is some metadata.
In each 3 request, there will be 2 request failed due to FileData empty.
Client:
var client = new HttpClient(new WebRequestHandler());
using (var content = new MultipartFormDataContent())
{
var guid = Guid.NewGuid().ToString();
var tmpFileName = string.Format("{0}{1}", guid, Path.GetExtension(fileName));
var dataContent = new ByteArrayContent(data);
content.Add(dataContent, guid, tmpFileName);
var optionContent = new ByteArrayContent(optionData);
optionContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("parameter") { Name = "optionsStr" };
content.Add(optionContent);
var response = client.PostAsync("http://test.com", content).Result;
}
Server:
[HttpPost]
public async Task<HttpResponseMessage> UploadDocument(string dataStr)
{
string rootPath = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Documents");
MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(rootPath);
await Request.Content.ReadAsMultipartAsync(provider);
if (provider.FileData == null || provider.FileData.Count == 0)
throw new Exception("There is no file in the current request of httpcontext.");
}
After sniffing using Wiresharks, found out the actual file data was misplaced in the preamble section of the multi-part structure:
error request:
wireshark snapshot1
correct request:
wireshark snapshot2
any clue what may cause such behavior?

Not receiving JSON data from Microsoft FaceAPI

I am trying to get JSON data from a picture using Microsoft's FaceAPI. I am receiving a StatusCode OK, but am not getting anything significant back. I have verified that the MemoryStream has the right data (which I am getting from an Image control) by saving it to a file.
private async Task<string> GetJSON()
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "mykey");
// Request parameters
queryString["returnFaceId"] = "true";
queryString["returnFaceLandmarks"] = "false";
var uri = "https://api.projectoxford.ai/face/v1.0/detect?" + queryString;
HttpResponseMessage response;
// Request body
byte[] byteData = ImageToByte();
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync(uri, content);
}
return "";
}
private byte[] ImageToByte()
{
using (MemoryStream stream = new MemoryStream())
{
videoBox.Dispatcher.Invoke(delegate
{
var encoder = new PngBitmapEncoder();
var flippedBitmap = new TransformedBitmap();
flippedBitmap.BeginInit();
flippedBitmap.Source = (BitmapSource)videoBox.Source;
var transform = new ScaleTransform(-1, 1);
flippedBitmap.Transform = transform;
flippedBitmap.EndInit();
encoder.Frames.Add(BitmapFrame.Create(flippedBitmap));
encoder.Save(stream);
});
using (FileStream test = new FileStream("snapshot.bmp", FileMode.Create))
{
stream.Position = 0;
stream.CopyTo(test);
}
return stream.ToArray();
}
}
You'll want to call await response.Content.ReadAsStringAsync() to get the JSON.
Alternatively, you can use the Microsoft.ProjectOxford.Face NuGet package which does the plumbing for you, plus provide C# types thereby relieving you the tedium of parsing the JSON.
I am not a c# programmer but after looking at your code, method GetJSON is returning hard coded empty string that might be the cause you are not getting anything back from the server after invoking this method or second reason could be your asynchronous server configuration is not working properly thus its returning blank first and doing actual operation later.

Categories