javascript - How do I fix BadRequest error "Unable to read JSON request payload" when creating a OneDrive fold

admin2025-04-19  0

I am creating a dialog in Google Sheets that generates and uploads a report to OneDrive. The user may need to create a folder in OneDrive through the dialog. However, when making the API request, I am getting a "BadRequest" error.

I have tried performing the request on the Windows mand line using Curl. I have also tried using pure JS instead of the Google Script language. I am able to perform other actions such as searching OneDrive and uploading files.

// The GS code

var auth = "Bearer " + acc;

var options = {
    "method": "post",
    "headers": {
        "Authorization": auth,
        "Content-Type": "application/json"
    },
    "payload": {
        "name": "Test Folder",
        "folder": {},
        "@name.conflictBehavior": "rename"
    },
    "muteHttpExceptions": true
};

var reqUrl = ".0/me/drive/root/children";
var response = UrlFetchApp.fetch(reqUrl, options);
var json = JSON.parse(response);
Logger.log(json); 



// The JS code

function onAuthSuccess(acc) {
    var pNum = document.getElementById("projectnum").value;
    var pName = document.getElementById("address").value;
    var reqUrl = ".0/me/drive/root/children";
    var reqBody = {
        "name": "Test Folder",
        "folder": {},
        "@microsoft.graph.conflictBehavior": "rename"
    };
    var auth = "Bearer " + acc;

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        console.log(xhr.responseText);
    }
    xhr.open("POST", reqUrl, true);
    xhr.setRequestHeader("Authorization", auth);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.send(reqBody);
}



// The successful Curl mand

// curl ".0/me/drive/root/children" -X POST -H "Content-Type: application/json" -H %acc% -d "{'name':'Test Folder', 'folder':{}, '@microsoft.graph.conflictBehavior':'rename'}"

The Curl mand produces the expected result, which is to create a new folder called "Test Folder" in our OneDrive root directory.

Both the GS and JS code above produce the following error message:

{
    error = {
        code = BadRequest,
        innerError = {
            date = 2019 - 06 - 24 T20: 40: 52,
            request - id = #####################
        },
        message = Unable to read JSON request payload.Please ensure Content - Type header is set and payload is of valid JSON format.
    }
}

I am creating a dialog in Google Sheets that generates and uploads a report to OneDrive. The user may need to create a folder in OneDrive through the dialog. However, when making the API request, I am getting a "BadRequest" error.

I have tried performing the request on the Windows mand line using Curl. I have also tried using pure JS instead of the Google Script language. I am able to perform other actions such as searching OneDrive and uploading files.

// The GS code

var auth = "Bearer " + acc;

var options = {
    "method": "post",
    "headers": {
        "Authorization": auth,
        "Content-Type": "application/json"
    },
    "payload": {
        "name": "Test Folder",
        "folder": {},
        "@name.conflictBehavior": "rename"
    },
    "muteHttpExceptions": true
};

var reqUrl = "https://graph.microsoft./v1.0/me/drive/root/children";
var response = UrlFetchApp.fetch(reqUrl, options);
var json = JSON.parse(response);
Logger.log(json); 



// The JS code

function onAuthSuccess(acc) {
    var pNum = document.getElementById("projectnum").value;
    var pName = document.getElementById("address").value;
    var reqUrl = "https://graph.microsoft./v1.0/me/drive/root/children";
    var reqBody = {
        "name": "Test Folder",
        "folder": {},
        "@microsoft.graph.conflictBehavior": "rename"
    };
    var auth = "Bearer " + acc;

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        console.log(xhr.responseText);
    }
    xhr.open("POST", reqUrl, true);
    xhr.setRequestHeader("Authorization", auth);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.send(reqBody);
}



// The successful Curl mand

// curl "https://graph.microsoft./v1.0/me/drive/root/children" -X POST -H "Content-Type: application/json" -H %acc% -d "{'name':'Test Folder', 'folder':{}, '@microsoft.graph.conflictBehavior':'rename'}"

The Curl mand produces the expected result, which is to create a new folder called "Test Folder" in our OneDrive root directory.

Both the GS and JS code above produce the following error message:

{
    error = {
        code = BadRequest,
        innerError = {
            date = 2019 - 06 - 24 T20: 40: 52,
            request - id = #####################
        },
        message = Unable to read JSON request payload.Please ensure Content - Type header is set and payload is of valid JSON format.
    }
}
Share asked Jun 24, 2019 at 22:45 Anthony BeckerAnthony Becker 231 gold badge2 silver badges5 bronze badges 3
  • I think that providing the curl mand you tested will help users think of your solution. – Tanaike Commented Jun 24, 2019 at 22:59
  • This is a great first question, btw, Anthony. Really detailed and containing all the right info. – oligofren Commented Jun 24, 2019 at 23:12
  • 1 Thank you to both of you for the encouragement! This has been a great experience after reading stackoverflow Q&A for a long time. – Anthony Becker Commented Jun 25, 2019 at 22:51
Add a ment  | 

1 Answer 1

Reset to default 3

Your code has a basic problem: you don't poste valid JSON (even though your Header says so).

var reqBody = {
    "name": "Test Folder",
    "folder": {},
    "@microsoft.graph.conflictBehavior": "rename"
};

This is just a normal javascript object. Doing a .toString() on this would simply give you "[object Object]". You need to encode it to a USVString (basically a normal string), per the XHR docs. So to make it into something that the XHR#send() method handles, do this:

var reqBody = JSON.stringify({
    "name": "Test Folder",
    "folder": {},
    "@microsoft.graph.conflictBehavior": "rename"
});

The result is a string:

'{"name":"Test Folder","folder":{},"@microsoft.graph.conflictBehavior":"rename"}'

, which is far more usable :)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745057494a282490.html

最新回复(0)