c# redirect post request - c#

Trying to make a post request from one application to another using the same Form Data parameters that the first one received.
both application controller currently have same method:
public async Task<ActionResult> TestSet()
{
var inputString = Request.Form["inputString"];
var inputFile = Request.Files[0];
var resultString = await _service.Set(inputString, inputFile.FileName, inputFile.ContentType, inputFile.InputStream);
return new MyJsonResult(new
{
fileName = resultString
});
}
which return json string:
{"fileName": "someFileName.png"}
Trying to make the first method to be something like this
public async Task<ActionResult> TestSet()
{
var inputString = Request.Form["inputString"];
var inputFile = Request.Files[0];
if (!string.IsNullOrEmpty(_redirectUrl))
{
using (var client = new HttpClient())
{
HttpContent content = GetContentSomehow(this.Request); // this i have an issue with
var response = await client.PostAsync(_redirectUrl, content);
var responseString = await response.Content.ReadAsStringAsync();
return new MyJsonResult(responseString);
}
}
var resultString = await _service.Set(inputString, inputFile.FileName, inputFile.ContentType, inputFile.InputStream);
return new MyJsonResult(new
{
fileName = resultString
});
}
This could help to get ByteArrayContent for File only.
And this would probably work to get the non-file parameters into StringContent, but how to get both of them into single Content?
var jsonString = JsonConvert.SerializeObject(Request.Form.ToDictionary());
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");

Solved this issue by cominig StringContent and StreamContent into MultipartFormDataContent
public static MultipartFormDataContent GetMultipartFormData(HttpRequestBase req)
{
var formData = new MultipartFormDataContent();
//formData.Headers.ContentType.MediaType = "multipart/form-data";
foreach (var row in req.Form.ToDictionary())
{
formData.Add(new StringContent(row.Value), row.Key);
}
var file = req.Files[0];
StreamContent fileStreamContent = new StreamContent(file.InputStream);
fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType);
formData.Add(fileStreamContent, file.FileName, file.FileName);
return formData;
}

Related

Postman form-data: How to program it inside a HttpRequestMessage?

I am doing a request through postman to a specific url but I set the form-data type in order to get data to the site like this:
Now I want to program this request inside C# but everything I tried so far is returning a 400 Bad Request response. This is what I tried:
public async Task<CheckAccessTokenModel> CheckAccessTokenAsync(string accessToken)
{
string uriString = "someurl";
var uri = new Uri(uriString);
try
{
using(var httpClient = new HttpClient())
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = uri
};
var ClientId = ConfigurationAccessor.Configuration["WebCredentials:ClientId"];
var Secret = ConfigurationAccessor.Configuration["WebCredentials:Secret"];
var authString = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{ClientId}:{Secret}"));
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", authString);
MultipartFormDataContent content = new MultipartFormDataContent();
content.Add(new StringContent("token"), accessToken);
request.Content = content;
var response = await httpClient.SendAsync(request);
var checkTokenResponseData = await response.Content.ReadAsStringAsync();
//return new CheckAccessTokenModel { Active = true, Exp = 1647431224233 };
return JsonConvert.DeserializeObject<CheckAccessTokenModel>(checkTokenResponseData);
}
}
catch
{
return null;
}
}
I am doing it with the MultipartFormDataContent Object as suggested by many others here but it still won't work.
What can be the problem here?
EDIT: Wrong picture replaced
You can simply
request.Content = new StringContent($"token={accessToken}");
With form data I think it's something like this:
var data = new Dictionary<string, string>
{
{"token", acccessToken}
};
using var content = new FormUrlEncodedContent(data);
request.Content = content;

Uploading or sending a file with class data in json to WebAPI 2 in RESTful to WebAPI2 does not work

I am looking to upload a file with some associated data from a C# win app to a WebAPI 2.0 using HTTP POST, but the request does not seems to be created corectly, however when I try to send them seperately it works fine.
Below is Win App Code for Upload:
public bool Upload(string url, string filePath, string localFilename, string uploadFileName)
{
Boolean isFileUploaded = false;
HttpClient httpClient = new HttpClient();
var fileStream = File.Open(localFilename, FileMode.Open);
var fileInfo = new FileInfo(localFilename);
MultipartFormDataContent content = new MultipartFormDataContent();
content.Headers.Add("filePath", filePath);
content.Add(new StreamContent(fileStream), "\"file\"", string.Format("\"{0}\"", uploadFileName + fileInfo.Extension));
TestDocument td = new TestDocument();
td.Field1 = "ABC";
td.Field2 = "PQR";
string json = JsonConvert.SerializeObject(td);
var httpContent = new StringContent(json, Encoding.UTF8, "multipart/form-data");
content.Add(httpContent);
var result = httpClient.PostAsync(url, content);
return true;
}
At Api end:
[HttpPost]
public HttpResponseMessage Post([FromBody] Testdocument document)
{
HttpResponseMessage result = null;
Testdocument otd = new Testdocument();
otd = document;
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
var docfiles = new List<string>();
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];
var filePath = HttpContext.Current.Server.MapPath("~/App_Data/" + postedFile.FileName);
postedFile.SaveAs(filePath);
docfiles.Add(filePath);
}
result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
}
else
{
result = Request.CreateResponse(HttpStatusCode.BadRequest);
}
return result;
}
Any suggestions on this?

Post MultipartFormDataContent from Controller Action Method to WebApi Post method

In my ASP.NET MVC project, I am trying to post image from Controller action method to API controller method. Plan is to leverage this API to other clients to use to upload images.
I am able to successfully hit the API Post method from Controller Action method, but am not able to pass the image object.
Here is my HomeController.cs Upload Action Method
[HttpPost]
public async Task<ActionResult> Upload(FormCollection formCollection)
{
var baseUri = "http://localhost/api/Process";
HttpPostedFileBase file = Request?.Files[0];
if (file == null || (file.ContentLength <= 0) || string.IsNullOrEmpty(file.FileName))
return new EmptyResult();
string fileName = file.FileName;
byte[] fileBytes = new byte[file.ContentLength];
HttpContent stringContent = new StringContent(fileName);
HttpContent fileStreamContent = new StreamContent(file.InputStream);
HttpContent bytesContent = new ByteArrayContent(fileBytes);
using (var formDataContent = new MultipartFormDataContent())
{
formDataContent.Add(stringContent, "fileName", fileName);
formDataContent.Add(fileStreamContent, "inputStream", fileName);
formDataContent.Add(bytesContent, "fileBytes", fileName);
using (var httpClient = new HttpClient())
{
formDataContent.Headers.ContentType =
MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(baseUri, formDataContent);
var content = await response.Content.ReadAsStringAsync();
//Handle the response
}
}
return View("Result");
}
In my Process controller which is inheriting from ApiController, I have the following Post method
[HttpPost]
public async Task<string> Post([FromBody]MultipartFormDataContent formDataContent)
{
Task<string> imageContent = Request.Content.ReadAsStringAsync();
string body = imageContent.Result;
ImageResponse imageResponse = null;
//.................
//.................
return someValue
}
Here parameter formDataContent is always null and Request.Content.ReadAsStringAsync() is empty
Try sending from the Controller like this
//...other code removed for brevity
using (var form = new MultipartFormDataContent()) {
var stringContent = new StringContent("fileToUpload");
form.Add(stringContent, "fileToUpload");
var streamContent = new StreamContent(file.InputStream);
streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse(file.ContentType);
streamContent.Headers.ContentLength = file.ContentLength;
streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
Name = "fileToUpload",
FileName = file.FileName
};
form.Add(streamContent);
using (var httpClient = new HttpClient()) {
var response = await httpClient.PostAsync(baseUri, form);
//...other code removed for brevity
}
}
And then process it in the ApiController by extracting the information sent ...
[HttpPost]
public async Task<IHttpActionResult> Post() {
var content = Request.Content;
//get file name from content disposition
var fileName = content.Headers.ContentDisposition.FileName;
//Get file stream from the request content
var fileStream = await content.ReadAsStreamAsync();
//...other code removed for brevity
return Ok();
}
Referenced this answer : Upload image using HttpClient

How to send a file and form data with HttpClient in C#

How can I send a file and form data with the HttpClient?
I have two ways to send a file or form data. But I want to send both like an HTML form. How can I do that? Thanks.
This is my code:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var client = new HttpClient();
var requestContent = new MultipartFormDataContent();
filename = openFileDialog1.FileName;
array = File.ReadAllBytes(filename);
var imageContent = new ByteArrayContent(array);
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("audio/*");
requestContent.Add(imageContent, "audio", "audio.wav");
var values = new Dictionary<string, string>
{
{ "token", "b53b99534a137a71513548091271c44c" },
};
var content = new FormUrlEncodedContent(values);
requestContent.Add(content);
var response = await client.PostAsync("localhost", requestContent);
var responseString = await response.Content.ReadAsStringAsync();
txtbox.Text = responseString.ToString();
}
Here's code I'm using to post form information and a csv file
using (var httpClient = new HttpClient())
{
var surveyBytes = ConvertToByteArray(surveyResponse);
httpClient.DefaultRequestHeaders.Add("X-API-TOKEN", _apiToken);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var byteArrayContent = new ByteArrayContent(surveyBytes);
byteArrayContent.Headers.ContentType = MediaTypeHeaderValue.Parse("text/csv");
var response = await httpClient.PostAsync(_importUrl, new MultipartFormDataContent
{
{new StringContent(surveyId), "\"surveyId\""},
{byteArrayContent, "\"file\"", "\"feedback.csv\""}
});
return response;
}
This is for .net 4.5.
Note the \" in the MultipartFormDataContent. There is a bug in MultipartFormDataContent.
In 4.5.1 MultipartFormDataContent wraps the data with the correct quotes.
Update: This link to the bug no longer works since the have retired Microsoft Connect.
Here's code I'm using a method to send file and data from console to API
static async Task uploaddocAsync()
{
MultipartFormDataContent form = new MultipartFormDataContent();
Dictionary<string, string> parameters = new Dictionary<string, string>();
//parameters.Add("username", user.Username);
//parameters.Add("FullName", FullName);
HttpContent DictionaryItems = new FormUrlEncodedContent(parameters);
form.Add(DictionaryItems, "model");
try
{
var stream = new FileStream(#"D:\10th.jpeg", FileMode.Open);
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(#"http:\\xyz.in");
HttpContent content = new StringContent("");
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "uploadedFile1",
FileName = "uploadedFile1"
};
content = new StreamContent(stream);
form.Add(content, "uploadedFile1");
client.DefaultRequestHeaders.Add("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.dsfdsfdsfdsfsdfkhjhjkhjk.vD056hXETFMXYxOaLZRwV7Ny1vj-tZySAWq6oybBr2w");
var response = client.PostAsync(#"\api\UploadDocuments\", form).Result;
var k = response.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
}
}

Post call for xml content using HttpClient

How to make xml content compatible with HttpClient's PostAsync operation for the content and where do you specify the headers for Content-Type = application/xml.
Error -> Cannot convert string to HttpContent
public async Task GetCustomersAsync(string firstname, string lastname)
{
using (var client = new HttpClient())
{
var content = "<soapenv:Envelope xmlns:xsi...";
var response = await client.PostAsync("https://domain.com/scripts/WebObj.exe/Client.woa/2/ws/ABC", content);
var responseString = await response.Content.ReadAsStringAsync();
}
}
My guess is what you want to do is the following:
public async Task<string> GetCustomersAsync(string firstname, string lastname)
{
using (var client = new HttpClient())
{
var content = new StringContent("<soapenv:Envelope xmlns:xsi...", Encoding.UTF8, "application/xml");;
var response = await client.PostAsync("https://example.com/scripts/WebObj.exe/Client.woa/2/ws/ABC", content);
return await response.Content.ReadAsStringAsync();
}
}
OR
using (var request = new HttpRequestMessage { RequesteUri = new Uri("POST_URL"), Method = HttpMethod.Post })
{
var content = new StringContent("<soapenv:Envelope xmlns:xsi...");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
}
You can refer here to more information about other Content types that can be created and passed.
To specifically request xml content in response you must define the content type in the header of the content. The MediaTypeHeaderValue is parsed and set in the ContentType property of the content Headers. Here is a complete example of the code;
using (var client = new HttpClient())
{
var content = new StringContent(messageToPOST, Encoding.UTF8, "text/xml");
content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml");
response = await client.PostAsync(_uri, content);
responseMsg = await response.Content.ReadAsStringAsync();
}
The responseMsg property returned by the request as the response can be parsed as a string and otherwise converted to and validated as xml using an expression such as
XDocument xdoc = XDocument.Parse(responseMsg);
string xmlAsString = xdoc.ToString();

Categories