Forum Discussion
Hi TestKeks,
File upload using multipart requests is tricky because the request body needs to be formatted with extra info - boundaries, Content-Disposition header, etc., as shown here.
aqHttp does not have a built-in method to format multipart requests. You will need to either format the request body manually (add the boundaries, Content-... headers, etc. yourself), or use some external tool or library to send the request.
Do you have curl? If yes, you could just shell out to curl:
var url = "http://httpbin.org/post"; var fileName = "C:\\my_image.png";
var fieldName = "file"; // The name of the form field whose value is the file contents.
// Check with the admins of the targer server or its docs
// to learn what field name to use.
var command = aqString.Format("curl \"%s\" -F \"%s=@%s\"", url, fieldName, fileName);
// TC 12.0+ WshShell.Run(command, 0, true);
// TC <= 11.x
// Sys.OleObject("WScript.Shell").Run(command, 0, true);
// TODO: check response status and content if needed
Alternatively, if you have .NET Framework 4.5+, you can use its HttpClient and MultipartFormDataContent classes as shown in the following example. You need to add the System.Net.Http assembly to Tools > Current Project Properties > CLR Bridge to use this code.
var url = "http://httpbin.org/post"; var fileName = "C:\\my_image.png"; var fieldName = "file"; // The name of the form field whose value is the file contents.
// Check with the admins of the targer server or its docs
// to learn what field name to use.
// Read file contents as a byte array
var fileData = dotNET.System_IO.File.ReadAllBytes(fileName);
// Configure multipart request
var httpClient = dotNET.System_Net_Http.HttpClient.zctor();
var formData = dotNET.System_Net_Http.MultipartFormDataContent.zctor();
formData.Add_3(dotNET.System_Net_Http.ByteArrayContent.zctor(fileData),
fieldName,
aqFileSystem.GetFileName(fileName));
// Send the request and check the response
var response = httpClient.PostAsync(url, formData).Result;
if (response.IsSuccessStatusCode)
{
Log.Message("success!")
}
Log.Message("Status: " + response.StatusCode.value__); // e.g. 200 or 400
Log.Message("Response contents - see Additional Info", response.Content.ReadAsStringAsync().Result);
Hi Helen,
thanks for your reply. Our messages have crossed.
Thank you and best regards
Related Content
Recent Discussions
- 21 minutes ago
- 6 hours ago