Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get Streams using batched requests, Length always zero #945

Open
greynoO opened this issue Nov 21, 2024 · 3 comments
Open

Unable to get Streams using batched requests, Length always zero #945

greynoO opened this issue Nov 21, 2024 · 3 comments

Comments

@greynoO
Copy link

greynoO commented Nov 21, 2024

Describe the bug

When using BatchRequestContentCollection (or BatchRequestContent) to request a stream content it only returns an empty stream (Length = 0). This happens regardless of whether there is a single or multiple requests in the collection.

var batchRequestContentCollection = new BatchRequestContentCollection(_graphClient);
var driveItemRequest = _graphClient.Drives[driveId].Items[driveItemId].Content.ToGetRequestInformation();
var requestId = await batchRequestContentCollection.AddBatchRequestStepAsync(driveItemRequest);
var batchResponse = await _graphClient.Batch.PostAsync(batchRequestContentCollection);
var stream = await batchResponse.GetResponseStreamByIdAsync(requestId);

I also tried to just use GetResponseByIdAsync, Header information says

Content-Type: application/octet-stream
Content-Length: 0

This does not occur when doing a single request without batching:

var stream = await _graphClient.Drives[driveId].Items[driveItemId].Content.GetAsync();

The stream gets returned correctly then.

Expected behavior

Batched requests should return stream content correctly.

How to reproduce

var batchRequestContentCollection = new BatchRequestContentCollection(_graphClient);
var driveItemRequest = _graphClient.Drives[driveId].Items[driveItemId].Content.ToGetRequestInformation();
var requestId = await batchRequestContentCollection.AddBatchRequestStepAsync(driveItemRequest);
var batchResponse = await _graphClient.Batch.PostAsync(batchRequestContentCollection);
var stream = await batchResponse.GetResponseStreamByIdAsync(requestId);

SDK Version

3.2.1

Latest version known to work for scenario above?

don't know

Configuration

  • OS: Windows 11
  • x64
@greynoO greynoO added status:waiting-for-triage An issue that is yet to be reviewed or assigned type:bug A broken experience labels Nov 21, 2024
@greynoO
Copy link
Author

greynoO commented Nov 21, 2024

Info: It works when building the batched request manually with HttpClient.

var batchRequests = new List<object>();

var url = $"/drives/{item.DriveId}/items/{item.DriveItemId}/content";
var requestId = Guid.NewGuid().ToString();

batchRequests.Add(new
{
    id = requestId,
    method = "GET",
    url = url
});

var batchRequestBody = new
{
    requests = batchRequests
};

var batchResponse = await _httpClient.PostAsync(
    "$batch",
    new StringContent(
        System.Text.Json.JsonSerializer.Serialize(batchRequestBody),
        Encoding.UTF8,
        "application/json"
    )
);

So there might be something wrong with BatchRequestContent?

The /content call returns a 302 status with a Location header to the actual file.

{"responses": [{"id":"<someId>","status":302,"headers":{"Location":"<uri>","Cache-Control":"no-store, no-cache","Content-Type":"application/octet-stream"},"body":""}]}

I guess that only the empty body is evaluated?

@andrueastman
Copy link
Member

Thanks for raising this @greynoO

It looks like due to the redirect response there is no response body returned when calling this specific API.

Are you able to obtain the location header if you change your code to fetch like this?

var responseMessage = await batchResponse.GetResponseByIdAsync(requestId);
var locationHeader = responseMessage.Headers.Location;

@andrueastman andrueastman added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed status:waiting-for-triage An issue that is yet to be reviewed or assigned labels Dec 3, 2024
@greynoO
Copy link
Author

greynoO commented Dec 8, 2024

Thanks for raising this @greynoO

It looks like due to the redirect response there is no response body returned when calling this specific API.

Are you able to obtain the location header if you change your code to fetch like this?

var responseMessage = await batchResponse.GetResponseByIdAsync(requestId);
var locationHeader = responseMessage.Headers.Location;

I get the location header, but of course it should be done automatically when calling GetResponseStreamByIdAsync?

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants