Skip to content

Graph client's batch request builder POST/PATCH request not working #833

Closed

Description

Describe the bug

I am trying to create multiple sharepoint list items in a list usign the batch request builder. Bu t throws API 400 error. Below is my implementation in the How to reproduce section

Expected behavior

I expect it to create all the records as it uses the jsonwriter and the method write_object which the actual ListItem also uses

How to reproduce

creds = ClientSecretCredential(
        tenant_id=settings.tenant_id, client_id=settings.app_id, client_secret=settings.client_secret
    )

client = GraphServiceClient(credentials=credentials, scopes=['https://graph.microsoft.com/.default'])

request_body = ListItem(
	fields = FieldValueSet(
		additional_data = {
				"title" : "Widget",
				"color" : "Purple",
				"weight" : 32,
		}
	),
)
request_info1: RequestInformation = client.sites.by_site_id(settings.site_id)
            .lists.by_list_id(settings.list_id)
            .items.to_post_request_information(body=request_body)

batch_request_content: BatchRequestContent = BatchRequestContent()
# using the requests setter
batch_request_content.requests = [
        BatchRequestItem(
            request_information=request_info1,
        ),
    ]

batch_response_content: BatchResponseContent = await client.batch.post(
        batch_request_content=batch_request_content
    )

for response in batch_response_content.responses.values():
        print(response.status)
        print(response.body)
        print(response.headers)

SDK Version

No response

Latest version known to work for scenario above?

No response

Known Workarounds

No response

Debug output

Click to expand log ```
</details>


### Configuration

_No response_

### Other information

_No response_

Activity

  1. added
    status:waiting-for-triage
    type:bug
    on Feb 18, 2025
  2. chandra447 commented on Feb 18, 2025

    @chandra447
    Author

    Vincent Biret (@baywet) i have created this issue to continue the conversation
    microsoftgraph/msgraph-sdk-python#915 (comment)

  3. chandra447 commented on Feb 18, 2025

    @chandra447
    Author

    i also want to add that, when returning the batchRequestBuilder it uses the msgraph_core where the url_template is formatted wrong as it adds an additional / based on this line which would result in https://graph.microsoft.com/v1.0//$batch

    self.url_template = f"{self._request_adapter.base_url}/$batch"

  4. chandra447 commented on Feb 19, 2025

    @chandra447
    Author

    Vincent Biret (@baywet) this thing was fixed when i manually overide the content of the batchrequest information as follows after this line in msgraph_core/batch_request_builder.

    My take would be the serialization of the batchRequestItem needs to be handled

    request_info.content = b'{"requests": [{"id": "8fb651a7-8f45-495c-a266-301aa14c91a4", "headers":{"Content-Type": "application/json"},"body":{ "fields": {"title": "widget", "color": "purple", }},"method": "POST", "url": "/sites/{{site_id}}/lists/{{list_id}}/items", "depends_on": []}]}'
  5. baywet commented on Feb 19, 2025

    @baywet
    Member

    Hi Chandra Doredla (@chandra447)
    Thank you for using the SDK and for reaching out.

    i also want to add that, when returning the batchRequestBuilder it uses the msgraph_core where the url_template is formatted wrong as it adds an additional / based on this line which would result in https://graph.microsoft.com/v1.0//$batch

    self.url_template = f"{self._request_adapter.base_url}/$batch"

    Would you mind sending a pull request to check whether the base url ends with a / before adding one please?

    @baywet this thing was fixed when i manually overide the content of the batchrequest information as follows after this line in msgraph_core/batch_request_builder.

    My take would be the serialization of the batchRequestItem needs to be handled

    request_info.content = b'{"requests": [{"id": "8fb651a7-8f45-495c-a266-301aa14c91a4", "headers":{"Content-Type": "application/json"},"body":{ "fields": {"title": "widget", "color": "purple", }},"method": "POST", "url": "/sites/{{site_id}}/lists/{{list_id}}/items", "depends_on": []}]}'

    Can you please share the payload you're currently getting for context?

  6. added
    status:waiting-for-author-feedback
    and removed
    status:waiting-for-triage
    on Feb 19, 2025
  7. chandra447 commented on Feb 19, 2025

    @chandra447
    Author

    Hi Vincent Biret (@baywet) ,
    this is the actual payload of the POST method of batch_request_builder, which doesn't add the payload and the headers

    Content: b'{"requests": [{"id": "1af2c34c-b891-4980-b8b8-4afef91e6086", "method": "POST", "url": "/sites/816366c1-bb5f-474c-b5f0-3ed59438300a/lists/cab71504-42b9-484d-a267-931c86c1ffc2/items", "depends_on": []}]}
  8. added and removed
    status:waiting-for-author-feedback
    on Feb 19, 2025
  9. baywet commented on Feb 19, 2025

    @baywet
    Member

    Thank you for the additional information.

    Tracing what should happen:

    1. The call to BatchRequestContent calls add request information
    2. Which in turns calls the item constructor
    3. Which sets the body

    Can you please:

    1. double check the request_information1 has a content? (to check the problem is with batch and not the fluent API/models)
    2. check that the batch request item has a body when you new it up from a request information with a body?
  10. added
    status:waiting-for-author-feedback
    and removed on Feb 19, 2025
  11. 9 remaining items

  12. added and removed
    status:waiting-for-author-feedback
    on Feb 19, 2025
  13. baywet commented on Feb 19, 2025

    @baywet
    Member

    Thanks for the additional information.

    This method is meant to be private to that class I don't think it's a good idea to take a dependency on it.

    Do you think we have alternatives here?

  14. added
    status:waiting-for-author-feedback
    and removed on Feb 19, 2025
  15. chandra447 commented on Feb 19, 2025

    @chandra447
    Author

    that method just needs a small change but not a major one
    at line
    needs to be changed to

    self.writer[key] = temp_writer.writer if temp_writer.writer else temp_writer.value
  16. added and removed
    status:waiting-for-author-feedback
    on Feb 19, 2025
  17. baywet commented on Feb 20, 2025

    @baywet
    Member

    FYI we've migrated all the kiota dependencies in python to https://github.com/microsoft/kiota-python to ease up the coordination between packages when making changes.

  18. added
    status:waiting-for-author-feedback
    and removed on Feb 20, 2025
  19. miyoyo commented on May 5, 2025

    @miyoyo

    Posting values in a batch is still broken, please keep this open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:waiting-for-author-feedbacktype:bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions