microsoftgraph / msgraph-sdk-javascript

Microsoft Graph client library for JavaScript
https://graph.microsoft.com
MIT License
756 stars 230 forks source link

Cannot upload file into OneDrive (content type error) #1276

Open jakubriegel opened 1 year ago

jakubriegel commented 1 year ago

Bug Report

Prerequisites

For more information, see the CONTRIBUTING guide.

Description

Cannot make a PUT request to upload file into OneDrive. The API returns Unable to read JSON request payload error. The docs here say to use text/plain content type, but the library here has it hardcoded to JSON.

Console Errors: [Is there any console error]

{
  statusCode: 400,
  code: 'BadRequest',
  requestId: '6418fb30-de6e-4b41-a53c-7e96eebc348b',
  date: 2023-05-12T07:51:14.000Z,
  body: '{"code":"BadRequest","message":"Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.","innerError":{"date":"2023-05-12T09:51:14","request-id":"6418fb30-de6e-4b41-a53c-7e96eebc348b","client-request-id":"8142eabc-e22d-a7eb-c8f6-be9ebc37899f"}}',
  headers: Headers {
    [Symbol(map)]: [Object: null prototype] {
      'transfer-encoding': [ 'chunked' ],
      'content-type': [ 'application/json' ],
      'content-encoding': [ 'gzip' ],
      vary: [ 'Accept-Encoding' ],
      'strict-transport-security': [ 'max-age=31536000' ],
      'request-id': [ '6418fb30-de6e-4b41-a53c-7e96eebc348b' ],
      'client-request-id': [ '8142eabc-e22d-a7eb-c8f6-be9ebc37899f' ],
      'x-ms-ags-diagnostic': [
        '{"ServerInfo":{"DataCenter":"West Europe","Slice":"E","Ring":"5","ScaleUnit":"002","RoleInstance":"AM2PEPF0000BE20"}}'
      ],
      date: [ 'Fri, 12 May 2023 09:51:13 GMT' ],
      connection: [ 'close' ]
    }
  }
}

Screenshots: [If applicable, add screenshots to help explain your problem]

Steps to Reproduce

const filename = 'file.csv';
const content = 'content';
await.client.api(`/drives/${this.driveId}/items/${this.folderId}:/${filename}:/content`).put(content);

Expected behavior: PUT is succesfully sent and file is uploaded

Actual behavior: PUT was sent with invalid content type

Additional Context

Add any other context about the problem here..

Usage Information

check log above

jakubriegel commented 1 year ago

I've changed my code so that it sets content type explicitly:

const filename = 'file.csv';
const content = 'content';
await.client.api(`/drives/${this.driveId}/items/${this.folderId}:/${filename}:/content`)
    .headers({'Content-Type': 'text/plain'})
    .put(content);

The request now matches documentation, but I still get an error:

{
  statusCode: 400,
  code: 'BadRequest',
  requestId: '69596a36-0fdd-44f1-aa12-02d1b7373707',
  date: 2023-05-12T08:14:24.000Z,
  body: '{"code":"BadRequest","message":"Entity only allows writes with a JSON Content-Type header.","innerError":{"date":"2023-05-12T10:14:24","request-id":"69596a36-0fdd-44f1-aa12-02d1b7373707","client-request-id":"9d35cfcc-21f3-74ba-78cc-ea0accc4d93a"}}',
  headers: Headers {
    [Symbol(map)]: [Object: null prototype] {
      'transfer-encoding': [ 'chunked' ],
      'content-type': [ 'application/json' ],
      'content-encoding': [ 'gzip' ],
      vary: [ 'Accept-Encoding' ],
      'strict-transport-security': [ 'max-age=31536000' ],
      'request-id': [ '69596a36-0fdd-44f1-aa12-02d1b7373707' ],
      'client-request-id': [ '9d35cfcc-21f3-74ba-78cc-ea0accc4d93a' ],
      'x-ms-ags-diagnostic': [
        '{"ServerInfo":{"DataCenter":"West Europe","Slice":"E","Ring":"5","ScaleUnit":"001","RoleInstance":"AM4PEPF000251A9"}}'
      ],
      date: [ 'Fri, 12 May 2023 10:14:23 GMT' ],
      connection: [ 'close' ]
    }
  }
}
sebastienlevert commented 1 year ago

@koros, can you have a look?

vladtsit commented 1 year ago

Hi @jakubriegel have you solved the problem?

jakubriegel commented 1 year ago

@vladtsit No, as this issue needs to be resolved on library level. But I have a workaround.

I used https://learn.microsoft.com/pl-pl/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online. I create an upload url with the Client provided by Microsoft and then use received upload url to PUT the file with Axios.

cc: @sebastienlevert @koros

tboulis commented 1 year ago

Im facing the same issue, any progress so far?