JsonReaderException with MessagesRequestBuilder.GetAsync

Scott Waye 25 Reputation points
2023-09-27T22:24:16.5266667+00:00

Hi,

Attempting to retrieve a particular messages results in this exception, unfortunately I can't share the message as it is confidential. Please can you advise why the Graph SDK would be attempting to parse invalid json?

Thanks,

ype: System.Text.Json.JsonReaderException
Message: Expected depth to be zero at the end of the JSON payload. There is an open JSON object or array that should be closed. LineNumber: 0 | BytePositionInLine: 247.
Source: System.Text.Json
TargetSite: Void ThrowJsonReaderException(System.Text.Json.Utf8JsonReader ByRef, System.Text.Json.ExceptionResource, Byte, System.ReadOnlySpan`1[System.Byte])
StackTrace:    at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.JsonDocument.Parse(ReadOnlySpan`1 utf8JsonSpan, JsonReaderOptions readerOptions, MetadataDb& database, StackRowStack& stack)
   at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedArrayPoolBytes, PooledByteBufferWriter extraPooledByteBufferWriter)
   at System.Text.Json.JsonDocument.Parse(Stream utf8Json, JsonDocumentOptions options)
   at Microsoft.Kiota.Serialization.Json.JsonParseNodeFactory.GetRootParseNode(String contentType, Stream content)
   at Microsoft.Kiota.Abstractions.Serialization.ParseNodeProxyFactory.GetRootParseNode(String contentType, Stream content)
   at Microsoft.Kiota.Abstractions.Serialization.ParseNodeFactoryRegistry.GetRootParseNode(String contentType, Stream content)
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.GetRootParseNode(HttpResponseMessage response)
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory`1 factory, Dictionary`2 errorMapping, CancellationToken cancellationToken)
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory`1 factory, Dictionary`2 errorMapping, CancellationToken cancellationToken)
   at Microsoft.Graph.Users.Item.MailFolders.Item.Messages.MessagesRequestBuilder.GetAsync(Action`1 requestConfiguration, CancellationToken cancellationToken)
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,015 questions
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 41,941 Reputation points
    2023-09-29T07:46:14.4566667+00:00

    Hi @Scott Waye

    Try changing the error code snippet to the following:

    var messages = await graphClient.Users["{user id}"].MailFolders["Inbox"].Messages.GetAsync((requestConfiguration) =>
    {
        requestConfiguration.QueryParameters.Expand = new string[] { "attachments" };
        requestConfiguration.QueryParameters.Top = 1;
    });
    
    if (messages == null)
    {
        return;
    }
    
    var pageIterator = PageIterator<Message, MessageCollectionResponse>
        .CreatePageIterator(
            graphClient,
            messages,
            // Callback executed for each item in the collection
            (msg) =>
            {
                Console.WriteLine(msg.Id);
                return true;
            },
            (req) =>
            {
                return req;
            });
    
    await pageIterator.IterateAsync();
    

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.