sendgrid-csharp: Client Throws 'Error while copying content to a stream' on 401 API Response
When making a call to SendGridClient.SendEmailAsync using a deliberately invalid API Key, rather than receiving a Response with a StatusCode of 401 Unauthorized, an exception is thrown:
System.Net.Http.HttpRequestException occurred
HResult=0x80131620
Message=Error while copying content to a stream.
Source=<Cannot evaluate the exception source>
StackTrace:
at System.Net.Http.HttpContent.<LoadIntoBufferAsyncCore>d__49.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at SendGrid.SendGridClient.<MakeRequest>d__23.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at SendGrid.SendGridClient.<RequestAsync>d__24.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at SendGrid.SendGridClient.<SendEmailAsync>d__25.MoveNext()
Technical details:
- sendgrid-csharp Version: 9.9.0
- Azure Function running Net462
Looking at the data received from SendGrid, this may or may not be due to the fact that the Content-Length header is > 0 but there is no response content:

About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 18
- Comments: 62 (14 by maintainers)
Commits related to this issue
- Ensure 401 is returned instead of incorrect exception https://github.com/sendgrid/sendgrid-csharp/issues/648 — committed to tedvanderveen/sendgrid-csharp by tedvanderveen 7 years ago
Any updates on this? I have just experienced this same issue with version 9.9.0 of the SendGrid client library - wasted a lot of time trying to figure out what was going on before I realized I had just accidentally removed a character from the API key!
I’ve had the same error after moving a website to Azure.
I fixed it by changing SendGridClient.cs, MakeRequest method.
Instead of:
to
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);Oh, I see but disagree. Getting a response of ‘Error while copying content to a stream’ when the failure is an invalid API – well this response does not help any developer.
From: childish-sambino [mailto:notifications@github.com] Sent: 02 September 2020 23:19 To: sendgrid/sendgrid-csharp sendgrid-csharp@noreply.github.com Cc: AndreduToit dutoita1011@gmail.com; Comment comment@noreply.github.com Subject: Re: [sendgrid/sendgrid-csharp] Client Throws ‘Error while copying content to a stream’ on 401 API Response (#648)
@oatsoda https://github.com/oatsoda This is something the needs to be handled via support so I recommend opening a new ticket there. The engineers that manage these libraries do not handle backend issues and support is the path forward.
For the item related to this library handling invalid API responses, I’m not convinced this library should be doing something different. It would basically be just failing in a different way. If the backend issue is resolved and a proper response is returned then there would be no special error handling here.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sendgrid/sendgrid-csharp/issues/648#issuecomment-685805893 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AHOBDK2OV3IM34ZCGYSMOFDSDZO5RANCNFSM4EJCAKXQ .
@oatsoda This is something the needs to be handled via support so I recommend opening a new ticket there. The engineers that manage these libraries do not handle backend issues and support is the path forward.
For the item related to this library handling invalid API responses, I’m not convinced this library should be doing something different. It would basically be just failing in a different way. If the backend issue is resolved and a proper response is returned then there would be no special error handling here.
Oh, I see but respectfully disagree. Getting a response of ‘Error while copying content to a stream’ when the failure is an invalid API – well this response does not help any developer. The one thing that the library may do differently is to provide a response which would aid sendgrid developer customers to track down their mistakes more efficiently. Maybe something along the lines of “Error - API key …”?
+1 Same here.
Same problem
I can confirm some of the observations that @oatsoda made regarding this issue. Please note that I used StrongGrid instead of SendGrid’s C# client in order help determine if this issue is specific to the client or not:
Observation 1: Using the same email content included in @oatsoda’s sample code (including the large text and HTML content), I observed that the response from SendGrid’s API is
HTTP/1.1 401 Unauthorized(this is expected since the sample code is intentionally using a bogus api key). I also observe that the content of the response is empty. Finally, I observe that the response contains the following header:Content-Length: 116which is contradictory with the fact that the response content is empty. This points to a server-side issue.Observation 2: Using the email content included in @oatsoda’s sample code with much smaller text and HTML content, I observed that the response from SendGrid’s API is
HTTP/1.1 401 Unauthorizedand the content of the response is{"errors":[{"message":"The provided authorization grant is invalid, expired, or revoked","field":null,"help":null}]}which is 116 characters long and is consistent with theContent-Length: 116header that I also observe in this scenario. There is no issue here, everything is working as expected.Observation 3: I am unable to reproduce the
Error while copying content to a streamexception that @oatsoda experienced when the response is empty. Since the only difference between my setup and his is that I am using an alternate C# client, this leads me to conclude that this issue is specific to SendGrid’s C# client.Conclusion: There seems to be two distinct issues:
Content-Length)Content-Lengthto determine if the body of the response contains any content.Similar to https://github.com/sendgrid/sendgrid-java/issues/259 and https://github.com/sendgrid/sendgrid-java/issues/641
Closing this as a non-library issue since it cannot/should not be remedied in client-side code. I’ve opened an internal ticket for tracking (reference ID: CL-2667). Please reference that ticket when contacting support regarding this issue.
Note that support and backend product teams do not use GitHub to prioritize work. Contacting support is the best way to push this issue along.
@thinkingserious Geeze, 2 years and it’s clearly a bug. So are you saying that in order to work on a “bug” you treat it like “new functionality”. I wish we could do that. When there is a production bug, we make a priority to hotfix it as soon as possible.
Our issue turned out to be that we had used our monthly allowance and needed to increase it. Would be nice to actually get the correct error msg back
We suffer from the same bug, please fix…
@oatsoda,
There is an internal bug happening here where the API is not returning the correct content-length. I’m going to circle back with product and engineering to see where we are on a fix. Thanks!
@thinkingserious The number was #1331298, but it is closed because they replied with:
Hence I am asking you for an update 😃