microsoft-graph-devx-api
microsoft-graph-devx-api copied to clipboard
[Snippet Generation] Issue handling non-json payloads
When trying to get code snippets for requests with non-json payload, Graph Explorer is not showing any for OpenAPI based client-libraries (snippets not available for C#, Go and PowerShell):
My assumption is that we have an issue when translating OData to OpenAPI as Java and JavaScript have snippets (haven't tested those though)
Looking into this, we have a few things to fix here.
- The openAPI decription does not match the expected API. This API expects a multipart/form-data request while the description describes it as json. This would lead/explain the snippet generation failure in this case.
- We may also not have a way for the conversion library to flag this api as not supporting json in the request but responding with json response.
- Kiota SDKs do not yet support multipart/form-data requests. This is tracked via https://github.com/microsoft/kiota/issues/220
Reference documentation at https://learn.microsoft.com/en-us/graph/api/section-post-pages?view=graph-rest-1.0
In C# a successful request can be made as below.
// Create multipart object with the relevant content
var multipartContent = new MultipartFormDataContent();
var htmlString =
"<!DOCTYPE html>\n<html>\n <head>\n <title>A page with <i>rendered</i> images and an <b>attached</b> file</title>\n <meta name=\"created\" content=\"2015-07-22T09:00:00-08:00\" />\n </head>\n <body>\n <p>Here's an image from an online source:</p>\n <img src=\"https://...\" alt=\"an image on the page\" width=\"500\" />\n <p>Here's an image uploaded as binary data:</p>\n <img src=\"name:imageBlock1\" alt=\"an image on the page\" width=\"300\" />\n <p>Here's a file attachment:</p>\n <object data-attachment=\"FileName.pdf\" data=\"name:fileBlock1\" type=\"application/pdf\" />\n </body>\n</html>";
var presentation = new StringContent(htmlString, Encoding.UTF8, "text/html");
multipartContent.Add(presentation,"Presentation");//needs a name
// We can add more httpcontent instance here if we wish to
// create a request information instance and make a request.
var requestInformation = graphClient.Me.Onenote.Sections["1-a465ed76-68b0-4070-a43c-46f1f7ffd5b5"].Pages.ToGetRequestInformation();
requestInformation.Headers.Add("Content-Type", multipartContent.Headers.ContentType.ToString());
requestInformation.HttpMethod = Method.POST;
requestInformation.Content = await multipartContent.ReadAsStreamAsync();
var errorMapping = new Dictionary<string, ParsableFactory<IParsable>> {
{"4XX", ODataError.CreateFromDiscriminatorValue},
{"5XX", ODataError.CreateFromDiscriminatorValue},
};
var result = await graphClient.RequestAdapter.SendAsync<OnenotePage>(requestInformation, OnenotePage.CreateFromDiscriminatorValue,errorMapping);
@andrueastman please create a new for the metadata portion which blocks moving on the rest of this.
Hello @andrueastman!
I'm currently working with this endpoint and experiencing the issues noted above. My C# code is the following:
var multipartContent = new MultipartFormDataContent("MyPartBoundary198374");
var htmlString =
"<!DOCTYPE html>\r\n<html>\r\n <head>\r\n <title>A page with a block of HTML</title>\r\n </head>\r\n <body>\r\n <p>This page contains some <i>formatted</i> <b>text</b>.</p>\r\n </body>\r\n</html>";
var presentation = new StringContent(htmlString, Encoding.UTF8, new MediaTypeHeaderValue("text/html"));
multipartContent.Add(presentation, "Presentation");
OnenotePage np = new OnenotePage();
np.Title = DateTime.Now.ToString("MMddyyyyHHmmss");
np.Content = await multipartContent.ReadAsByteArrayAsync();
var mes = await graphClient.Users[{userId}].Onenote.Sections[{sectionId}].Pages.PostAsync(np) ;
Based on the linked issue and this pull request, I think this code should work now, but I'm still getting the "Page create requests require the content to be multipart, with a presentation part." error.
Thoughts? I could easily be doing something incorrect. As a note, your suggestion here works, but I can't add a title to the page, and the documentation is a little shaky on it.
Thank you for your time!