pnp / pnpcore

The PnP Core SDK is a modern .NET SDK designed to work for Microsoft 365. It provides a unified object model for working with SharePoint Online and Teams which is agnostic to the underlying API's being called
https://aka.ms/pnp/coresdk/docs
MIT License
288 stars 188 forks source link

`BatchRequest.ResponseHttpStatusCode` is empty for no content request #1462

Closed StefanSchoof closed 1 month ago

StefanSchoof commented 2 months ago

Category

Describe the bug

I want to get a status code of an Delete Operation and lookup the ResponseHttpStatusCode in the request:

        var user = await context.Web.SiteUsers.FirstOrDefaultAsync(u => u.Id == principalId);
        Batch batch = context.NewBatch();
        user.DeleteBatch(batch);
        await context.ExecuteAsync(batch);
        if (batch.Requests[0].ResponseHttpStatusCode == 0)
        {
            throw new InvalidOperationException("ResponseHttpStatusCode should be 200");
        }

But the ResponseHttpStatusCode is always 0.

I took a look into the batch content and it is something like:

--batchresponse_b162bc00-56aa-475d-aef0-04e983426936
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 200 OK
CONTENT-TYPE: application/json;odata=nometadata;streaming=true;charset=utf-8

--batchresponse_b162bc00-56aa-475d-aef0-04e983426936--

I think I traced the origin to

https://github.com/pnp/pnpcore/blob/0b6106c9e28c0a8ba23bae1dd471c90f81d22b05/src/sdk/PnP.Core/Services/Core/BatchClient.cs#L1705-L1714

With no line starts with a { the variabel responseContentOpen never gets set to true and because of

https://github.com/pnp/pnpcore/blob/0b6106c9e28c0a8ba23bae1dd471c90f81d22b05/src/sdk/PnP.Core/Services/Core/BatchClient.cs#L1623

the lines

https://github.com/pnp/pnpcore/blob/0b6106c9e28c0a8ba23bae1dd471c90f81d22b05/src/sdk/PnP.Core/Services/Core/BatchClient.cs#L1657C1-L1664C30

not getting called for this request

Steps to reproduce

  1. Run the code
        var user = await context.Web.SiteUsers.FirstOrDefaultAsync(u => u.Id == principalId);
        Batch batch = context.NewBatch();
        user.DeleteBatch(batch);
        await context.ExecuteAsync(batch);
        if (batch.Requests[0].ResponseHttpStatusCode == 0)
        {
            throw new InvalidOperationException("ResponseHttpStatusCode should be 200");
        }
  2. Getting an error

Expected behavior

The ResponseHttpStatusCode is set for all BatchRequests.

Environment details (development & target environment)

Additional context

Thanks for your contribution! Sharing is caring.