I hate this API.... The documentation states:
POST /api/2/path/data/Documents/ HTTP/1.1
Content-Type: multipart/form-data
Content-Length: length of request body here
multi-part data here
So I created this:
public async Task<bool> UploadAsync(string path, string fileName, byte[] data)
{
EnforceBasicAuthentication();
var byteContent = new ByteArrayContent(data);
byteContent.Headers.Remove("Content-Type");
byteContent.Headers.Add("Content-Type", "multipart/form-data");
byteContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileName,
Name = "file"
};
var form = new MultipartFormDataContent
{
byteContent
};
var response = await _httpClient.PostAsync($"{_config.FtpUrl}/path/data/{_config.FtpPath}/{path}", form);
response.EnsureSuccessStatusCode();
return true;
}
But every time I run it, I get a 400 bad request error. It doesn't say anything other than that. Looking at my code, is there anything that looks wrong?
I have tried multiple solutions and none of them seem to work. Currently I have this:
public async Task<bool> UploadAsync(string path, string fileName, byte[] data)
{
EnforceBasicAuthentication();
var boundary = "--------------------------" + DateTime.Now.Ticks.ToString("x");
var bytesContent = new ByteArrayContent(data);
var form = new MultipartFormDataContent();
form.Add(bytesContent, "file", fileName);
form.Headers.Remove("Content-Type");
form.Headers.TryAddWithoutValidation("Content-Type", $"multipart/form-data; boundary= { boundary }");
bytesContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
var response = await _httpClient.PostAsync($"{_config.FtpUrl}/path/data/{_config.FtpPath}/{path}", form);
response.EnsureSuccessStatusCode();
return true;
}
But it still throws a 400 error. I have configured Fiddler and done a test in postman. The test in postman worked fine. Here is the request in fiddler:
POST https://app.smartfile.com/api/2/path/data/eOrdering/GRE017 HTTP/1.1
cache-control: no-cache
Postman-Token: 8359aae8-f4e2-4864-b81a-0169fe3f2b62
Authorization: Basic bleh
User-Agent: PostmanRuntime/7.1.1
Accept: /
Host: app.smartfile.com
cookie: csrftoken=rsDaPbfwG070rwBFlhIWVvULcAqGW0GB;
sessionid=a744788776bfef3c44faf6eb5e2a2931
accept-encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------606952503302468487113354
content-length: 334
Connection: close
----------------------------606952503302468487113354
Content-Disposition: form-data; name=""; filename="S200-220311-20180606095501.ord"
Content-Type: application/octet-stream
274 274/1 |PAULA|00220311|STD|||ASAP
1|23133|1313 131302 4.00|C|3.65|||12/06/18
----------------------------606952503302468487113354--
And this is the one generated by my code:
POST https://app.smartfile.com/api/2/path/data/eOrdering/GRE017 HTTP/1.1
Authorization: Basic blah
Content-Type: multipart/form-data; boundary=--------------------------8d5d140cf824278
Host: app.smartfile.com
Cookie: csrftoken=NYOgu61I6ddqBgudQEhWNr27fJUEQFi3;
sessionid=6994ad9254ebc46e0611e6ac308d6c55
Content-Length: 375
--52343329-f101-43a2-b4bb-bfa9a4073f6e
Content-Disposition: form-data; name=file; filename=3c3f1596-707b-4725-9766-25faf6124a35.rsl; filename*=utf-8''3c3f1596-707b-4725-9766-25faf6124a35.rsl
Content-Type: application/octet-stream
Fail|"Price Issue Number is invalid, must be greater than 0"|
1|23133|01/01/0001 00:00:00|0|0
--52343329-f101-43a2-b4bb-bfa9a4073f6e--
I still have no idea why it is failing :(