msgraph-sdk-python icon indicating copy to clipboard operation
msgraph-sdk-python copied to clipboard

Return headers of post request

Open mpikulaopt opened this issue 2 years ago • 3 comments

Hi,

is there any possibility of returning headers of completed post request? For example, when creating new team, headers should contain return code and ID of created object, but for now executing method teams.post returns just "None" string

Thank you from advance

mpikulaopt avatar Nov 29 '23 10:11 mpikulaopt

We need to provide a sample/documentation using the HeadersInspectionHandler at https://github.com/microsoft/kiota-http-python/blob/main/kiota_http/middleware/headers_inspection_handler.py to show how headers are read.

andrueastman avatar Apr 03 '24 12:04 andrueastman

@andrueastman any updates in this? We are struggling to expose the location header too. Can you point me as to how to implement it if not?

UPDATE: So It's not that hard to implement yourself. I imported

from kiota_http.middleware import HeadersInspectionHandler

Then instantiated an instance of it:

 headers_handler = HeadersInspectionHandler()

prior to making my request.

Once the request is made, I have in my instance of the HeadersInspectionHandler something like:

headers_handler.options._response_headers._headers['location']

I guess my next steps will be to make this into a nice middleware and all that, but it works! 🤷

demian-licht-hs avatar Jun 03 '24 08:06 demian-licht-hs

@demian-licht-hs

In dotnet it would look something like this

            var headersInspectionHandlerOption = new HeadersInspectionHandlerOption()
            {
                InspectResponseHeaders = true // specific you wish to collect reponse headers
            };
            //add the option
            var user = graphClient.Users["user-id"].GetAsync(requestConfiguration => requestConfiguration.Options.Add(headersInspectionHandlerOption));
            //use the key to get the header.
            var locationHeader = headersInspectionHandlerOption.ResponseHeaders["Location"];

So you just need to

  • initialize a headersInspectionHandlerOption instance and set InspectResponseHeaders to true.
  • Pass the instance to the options of your request using the request configuration object
  • Check the header using the key in the options object after the request returns.

We have a task to document this at https://github.com/MicrosoftDocs/openapi-docs/issues/90 for all languages, so it would be great if you could post a sample of the same in the issue as well

andrueastman avatar Jun 05 '24 10:06 andrueastman