Posting multiple binary files in a single POST with httpclient? - c#

How would one post multiple binaries in a single http POST operation using C# httpclient? I can't seem to find information on how to deal with httpcontent in this way - just doing a postASync with stream data twice?

Dug around a bit more and experimented, and finally found what seems like a working solution. I tried this on a test server with some images on HD - both sent, both worked. With two stream examples.
var client = new HttpClient();
var stream3 = new FileStream("saved.jpg", FileMode.Open);
var stream2 = new FileStream("saved2.jpg", FileMode.Open);
var dic = new Dictionary<string, string>();
dic.Add("Test1", "This was the first test.");
var addy = "http://posttestserver.com/post.php";
using (var content = new MultipartFormDataContent())
{
content.Add(new StreamContent(stream2), "s1", "Saved1.jpg");
content.Add(new StreamContent(stream3), "s2", "Saved2.jpg");
var response = await client.PostAsync(addy, content);
response.EnsureSuccessStatusCode();
string finalresults = await response.Content.ReadAsStringAsync();
}

It will depend on the implementation of the API that you are sending your files to but typically if multiple files are sent in a single POST request then it is sent as multipart/form-data. Have a look at this post for sending multipart/form-data through HttpClient.

Related

How to replicate Postman POST request in C#

I'm fairly new to .NET's HTTPClient class, hence kindly excuse if I sounded noob. I'm tryin to replicate Postman's POST request in C# .Net and written following code. However I'm not getting any response but StatusCode: 404. Could someone assist understanding where I'm going wrong?
Also I'd like to understand, how do set Body in following code.
var httpClient = new HttpClient
{
BaseAddress = new Uri("https://testURL.com"),
Timeout = TimeSpan.FromMinutes(10)
};
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("audio/wav"));
httpClient.DefaultRequestHeaders.Add("Authorization", "Basic ldjfdljfdlfjdsjfdsl");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("model", "Test"),
});
var result = httpClient.PostAsync("api/v1/recognize", content).Result;
Here is what I'm doing in Postman and it works:
"Params" in Postman refers to query parameters which are appended to the URL. You'll see that the URL in Postman contains the parameters you added in the "Params" tab:
However, it seems those are just dummy values you've entered so perhaps you don't need them? In any case, the way you add query parameters to the request for HttpClient is a little different as it needs to be added to the URL.
After that you also need to add the audio file as content to your request. At the moment you're setting the "Accept" header to "audio/wav" but you probably want to set the "Content-Type" header instead (or are you expecting a WAV file to be returned in the response too?).
As far as I can see this is what you're missing:
using (var httpClient = new HttpClient())
{
httpClient.Timeout = TimeSpan.FromMinutes(10);
// Set request headers
httpClient.DefaultRequestHeaders.Add("Authorization", "Basic ldjfdljfdlfjdsjfdsl");
// Set query parameters
var uriBuilder = new UriBuilder("https://testURL.com/api/v1/recognize");
uriBuilder.Query = "model=Test";
// Build request body
// Read bytes from the file being uploaded
var fileBytes = File.ReadAllBytes(wavFilePath);
// Create request content with metadata/headers to tell the
// backend which type of data (media type) is being uploaded
var byteArrayContent = new ByteArrayContent(fileBytes);
byteArrayContent.Headers.ContentType = MediaTypeHeaderValue.Parse("audio/wav");
// Wrap/encode the content as "multipart/form-data"
// See example of how the output/request looks here:
// https://dotnetfiddle.net/qDMwFh
var requestContent = new MultipartFormDataContent
{
{byteArrayContent, "audio", "filename.wav"}
};
var response = await httpClient.PostAsync(uriBuilder.Uri, requestContent);
}
I haven't tested this of course against your application, but it should be something along the lines of this. It might be that the backend doesn't expect "multipart/form-data" and just needs the "audio/wav". I can't see the output headers in your Postman screenshots, but if so, you can use byteArrayContent directly instead of wrapping it in MultipartFormDataContent.
Note: Don't use httpClient.PostAsync(...).Result. If you want to use the asynchronous method, you should await it. Depending on your code, using Result might give you problems if you're not careful. And remember to dispose the HttpClient after use (easiest solution is to use a using statement). If you plan on reusing the HttpClient for more requests, you can avoid disposing it until you're done.

Uploading .mp4 via HTTPS

I'm trying to upload an .mp4 file to Giphy.com's API. It says to send the file over as 'Binary' and I think I'm confused as what exactly they mean by that. Here's the docs if you scroll to the bottom at "Upload Endpoint". https://developers.giphy.com/docs/
Here's what I have right now.
I've tried multiple versions of this (using StringContent, MultipartFormDataContent, ByteArrayContent, HttpMessages... etc) and always get a '400 - Bad Request - No Source Url' (which the docs say isn't required if you upload you're own) which makes me believe the content isn't being recognized.
public async Task<HttpResponseMessage> UploadVideoAsync(StorageFile file)
{
using (var stream = await file.OpenStreamForReadAsync())
{
byte[] bytes = new byte[stream.Length];
await stream.ReadAsync(bytes, 0, (int)stream.Length);
Dictionary<string, string> dic = new Dictionary<string, string>
{
{ "file", Encoding.ASCII.GetString(bytes) },
{ "api_key", api_key }
};
MultipartFormDataContent multipartContent = new MultipartFormDataContent();
multipartContent.Add(new ByteArrayContent(bytes));
var response = await httpClient.PostAsync($"v1/gifs?api_key={api_key}", multipartContent);
var stringResponse = await response.Content.ReadAsStringAsync();
return response;
}
}
It seems that your code doesn't match {api_key} properly. You don't use the "dic" variable anywhere. You can try with v1/gifs?api_key=YOUR_API_KEY&file= instead. Where YOUR_API_KEY should be replaced by your API key obtained from giphy.
always get a '400 - Bad Request - No Source Url' (which the docs say isn't required if you upload you're own) which makes me believe the content isn't being recognized.
You need to apply a name for the ByteArrayContent. The document has shown that Request Parameters contains 'file: string (binary) required if no source_image_url supplied'.
The code should like the following:
MultipartFormDataContent multipartContent = new MultipartFormDataContent();
multipartContent.Add(new ByteArrayContent(bytes),"file");

Upload Reference File to Content ID

Has anyone attempted to do this? From what I understand Google doesn't provide a .NET client library for this (the only examples I found were in Python and PHP). The difficulty I'm having is that it seems to want a file upload and a JSON request body in the same request. Any info would be appreciated.
I was able to figure it out. In .NET (using HttpClient), you can use
using (MultipartFormDataContent content = new MultipartFormDataContent("----------" + DateTime.Now.ToString(CultureInfo.InvariantCulture)))
{
content.Add(new StringContent(JsonConvert.SerializeObject(uploadReferenceRequest), Encoding.UTF8, "application/json"));
StreamContent audioContent = new StreamContent(new MemoryStream(buffer));
audioContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
content.Add(audioContent);
using (HttpResponseMessage response = await this.Client.PostAsync(url, content))
{
string responseContent = await response.Content.ReadAsStringAsync();
}
}
The key is to set the content type on each section of data, on the first is the "application/json" and the actual sound data is "application/octet-stream". Hope someone else finds this useful.

.NET Core 1.0 read form-data posted file and put it in MultipartFormDataContent

I'm trying to fetch form-data sent to .NET Core API. The aim is to redirect the post request to another API.
I don't want the file to be stored locally but only "transmitted" again in a request.
Here is the code I'm trying.
MultipartFormDataContent multipartContent = new MultipartFormDataContent();
multipartContent.Add(new StringContent(Request.Form["id"]), "id");
var file = Request.Form.Files[0];
if (file.Length > 0)
{
var fileStreamContent = new StreamContent(Request.Form.Files[0].OpenReadStream());
multipartContent.Add(fileStreamContent, Request.Form.Files[0].FileName);
client.BaseAddress = new Uri("http://example.com:8000");
client.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage response = await client.PostAsync("/document/upload", multipartContent);
if (response.IsSuccessStatusCode)
{
retValue = await response.Content.ReadAsAsync<DocumentUploadResult>();
}
}
It seems Request.Form.Files[0].OpenReadStream() doesn't work, as the request received by remote api is empty. The openReadStream was suggested by VS2015 as the former .FileStream i used before .NET Core seems not to be present anymore.
Is my idea to try to create a StreamContent from the file actually a good idea? Is there any better type I could use to transmit my file accepted by MultripartFormDataContent?
In order to read a file from a form post, have your Action accept an IFormFile parameter:
public async Task<IActionResult> UploadFile(IFormFile file){
byte[] bytes = new byte[file.Length];
using (var reader = file.OpenReadStream())
{
await reader.ReadAsync(bytes, 0, (int)file.Length);
}
}

Upload file via POST call with C#, RestSharp, Redmine API

I'm developing a C# app that uses Redmine REST API, it uses RestSharp Client. All other REST calls I make work fine but I cannot find a way to upload attachments. I've widely searched the web and tried many solutions but nothing worked.
Redmine documentiation on attachments: http://www.redmine.org/projects/redmine/wiki/Rest_api#Attaching-files
The code actually looks like:
RestClient client = new RestClient("http://myclient/redmine/");
client.Authenticator = new HttpBasicAuthenticator("myuser", "mypsw");
var request2 = new RestRequest("uploads.json", Method.POST);
request2.AddHeader("Content-Type", "application/octet-stream");
request2.RequestFormat = RestSharp.DataFormat.Json;
byte[] dataToSend = File.ReadAllBytes(AddIssue.attach.Text);
request2.AddBody(dataToSend);
IRestResponse response2 = client.Execute(request2);
resultbox.Text = response2.Content;
If I execute it above nothing happens and the response is empty. If I remove line 7 (the AddBody), it actually works but of course nothing is uploaded, JSON response:
{
"upload": {
"token": "11."
}
}
So actually, the real question is what to punt in AddBody() to send the file as application/octet-stream. Since RestSharp also has a request.AddFile() method, I tried it too in different ways but nothing...
Any help much appreciated!
As I mentioned in my comment, it sounds like Redmine might have requirements similar to Dropbox. Here is the solution that worked for me (based on the question Upload to dropbox using Restsharp PCL):
public static void UploadFileToDropbox(string filePath)
{
RestClient client = new RestClient("https://api-content.dropbox.com/1/");
IRestRequest request = new RestRequest("files_put/auto/{path}", Method.PUT);
FileInfo fileInfo = new FileInfo(filePath);
long fileLength = fileInfo.Length;
request.AddHeader("Authorization", "Bearer INSERT_DEVELOPER_TOKEN_HERE");
request.AddHeader("Content-Length", fileLength.ToString());
request.AddUrlSegment("path", string.Format("Public/{0}", fileInfo.Name));
byte[] data = File.ReadAllBytes(filePath);
var body = new Parameter
{
Name = "file",
Value = data,
Type = ParameterType.RequestBody,
};
request.Parameters.Add(body);
IRestResponse response = client.Execute(request);
}
Also published as a Gist.
I know this isn't your exact situation, but hopefully it gives you some ideas.

Categories