2

I am building a web app using the Box API, but I'm having trouble simply creating a folder in the root directory. The token is set to a developer token which should be active.

The error I'm receiving now is Bad Request. What is wrong with this code? I'm also having trouble receiving authentication for the user, but I decided to tackle this first.

function createTestFolder() {
    $.ajax('https://api.box.com/2.0/folders', 
    {
        data: { name: 'CreatedFolderFromjQuery', parent: { id: '0' } },
        type: 'POST',
        beforeSend: function(xhr) {
            xhr.setRequestHeader('Authorization', 'Bearer ' + window.token);
        },
        contentType: 'json',
        success: function(data, status, xhr) {
            alert(status);
        },
        error: function(xhr, status, error) { alert(error); }
    });
}

EDIT : When I change the URL to https://box.com/api/1.0/folders, I seem to get a successful response. That is, the success function gets called, rather than the error function. However, the folder is still not uploaded to my account.

EDIT 2 : Using the curl command line and following the API documentation, I still receive the same error message. Here are the details:

{"type":"error", "status":400, "code":"bad_request", "context_info":{"errors":[{"reason":"invalid_parameter", "name":"entity_body", "message":"Invalid value ''{name:New Folder,'. Entity body should be a correctly nested resource attribute name/value pair"}]}, "help_url":"http://developers.box.com/docs/#errors", "message":"Bad Request", "request_id":"128521198353f4fc831c7e6"}
curl: (6) Could not resolve host: parent
curl: (3) [globbing] unmatched brace in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 2

1 Answer 1

2

Ah, I have solved my own question. I am not familiar with jQuery, and here was my mistake:

data: { name: 'CreatedFolderFromjQuery', parent: { id: '0' } },

With jQuery's AJAX API, data needs to be a string. I needed to serialize my POST data into JSON:

data: JSON.stringify({ name: 'CreatedFolderFromjQuery', parent: { id: '0' } }),

Once I realized this, and added the JSON.stringify() the request was sent properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.