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:

image

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 18
  • Comments: 62 (14 by maintainers)

Commits related to this issue

Most upvoted comments

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:

Line 250: 
HttpResponseMessage response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);

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: 116 which 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 Unauthorized and 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 the Content-Length: 116 header 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 stream exception 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:

  1. The server is not returning the expected content with the response in the scenario described in observation 1 (combined with the fact that it returns incorrect Content-Length)
  2. The client is not properly handling situations where the body of the response is empty and/or relying on the incorrect Content-Length to 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.

@thinkingserious Any fix on this, randomly started happening on our live system too since 26th May, no code was changed.

Getting the Unauth response if text email sent (key is correct) and getting ‘Error while copying content to a stream’ if HTML email is used.

at SendGrid.SendGridClient.d__23.MoveNext() in C:\Users\ethomas\Documents\GitHub\sendgrid-csharp\src\SendGrid\SendGridClient.cs:line 250

SendGrid ticket no. Request #2238173

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:

It looks like you’ve been working with one of our Engineers via the github repository. He is definitely your best resource for this particular issue. This bug falls a bit outside of the scope of Support, but regardless of whether it’s client related or API related, the engineer you were in contact with via GitHub should be able to assist.

Hence I am asking you for an update 😃