RestSharp: Added files not being recieved
Expected Behavior
Send file to specified URL for storage
Actual Behavior
RestRequest.Files.Add(…) states the file has been added, but when RestClient.Execute(RestRquest) goes, I get a response “No files received”. This worked on the previous version. No code has changed from either my platform or the WebAPI it’s communicating with. What has changed that might be causing this?
Steps to Reproduce the Problem
`private bool uploadPDF(SpoolDataRow pRow)
{
RestClient pClient = new RestClient(m_pLoginMgr.Server);
pClient.Authenticator = new HttpBasicAuthenticator(m_pLoginMgr.Username, m_pLoginMgr.Password);
RestRequest pPDFRequest = new RestRequest(Config.API_PUT_SPOOL_PDF(pRow.ID), Method.POST);
pPDFRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
byte[] arBytes = File.ReadAllBytes(pRow.PDF);
if (arBytes == null) return false;
pPDFRequest.Files.Add(new FileParameter()
{
Name = "SpoolFile",
Writer = pWriter =>
{
using (StreamReader pFile = new StreamReader(new MemoryStream(arBytes)))
{
pFile.BaseStream.CopyTo(pWriter);
}
},
FileName = Path.GetFileName(pRow.PDF),
ContentLength = arBytes.Length,
ContentType = "application/octet-stream"
});
IRestResponse pResponse = pClient.Execute(pPDFRequest);
return pResponse.StatusCode == System.Net.HttpStatusCode.OK;
}
`
Specifications
- Version: 106.2.1
- Platform: Windows 10, Autodesk Revit
- Subsystem:
StackTrace
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 23 (9 by maintainers)
I attempted to debug this but didn’t come up with anything. I’m also running into the same issue. On the receiving end the HttpContext.Current.Request.Files.Count is 0 (zero) even though I see (when debugging in the RestSharp code) that the stream is being written. I’m going to revert to the previous version (105.2.3), which works fine when posting a file to our api.
I know this is closed, but for those who may still be encountering this problem, here’s what I found and how we fixed it.
@bryanparke and I have been trying to upgrade to newer versions of RestSharp for a while now, and kept hitting this error on file uploads, which forced us to rollback to version 106.1.0 each time. Ultimately, we tracked this down to a call to
request.AddHeader("Content-Type", "application/json");Once we removed the call to add that header, files started coming through in the controller just fine.In our case we were usually expecting to be sending json content so we had always been attaching the Content-Type header whether sending json or uploading files, which had been working. But versions newer than 106.1.0 were no longer sending the files while adding the “Content-Type” header.
Hopefully this can help someone…
106.1.0
On Mon, Feb 12, 2018 at 10:11 AM, Alexey Zimarev notifications@github.com wrote:
– Geoff Overfield (720) 322-6242