how to call esri Rest API (createFolder) with esriRequest (POST options)

312
1
Jump to solution
01-12-2020 05:09 PM
JamesMoloney2
New Contributor II

Hi all,

I am struggling to create a folder with javascript 4.14 calling esriRequest

Here is an example function I am trying to get working... (myuser is just for example purposes)

function createProjectFolder(folderName){
        var url = "https://www.arcgis.com/sharing/rest/content/users/myuser/createFolder";
        var folderParams = {
            title: folderName
        };
        var headers = {
            "Access-Control-Allow-Origin": "*",
            "content-type": "multipart/form-data",
            "accept": "application/json"
        };

        esriRequest(url, {
            method: "post",
            headers: headers,
            content: folderParams,
            responseType: "json",
        }).then(function (response) {
            console.log(response);
        });

    };

I keep getting a these CORS errors

OPTIONS https://www.arcgis.com/sharing/rest/content/users/myuser/createFolder 405
(anonymous) @ dojo.js:695
e @ dojo.js:428
(anonymous) @ dojo.js:427
(anonymous) @ dojo.js:622
k @ dojo.js:431
(anonymous) @ dojo.js:622
D @ dojo.js:691
(anonymous) @ dojo.js:700
e @ dojo.js:428
(anonymous) @ dojo.js:427
n @ dojo.js:622
d @ dojo.js:136
then.a.then @ dojo.js:139
p @ dojo.js:622
(anonymous) @ dojo.js:622
k @ dojo.js:431
(anonymous) @ dojo.js:622
I @ dojo.js:700
(anonymous) @ dojo.js:684
e @ dojo.js:428
(anonymous) @ dojo.js:427
n @ dojo.js:622
d @ dojo.js:136
then.a.then @ dojo.js:139
p @ dojo.js:622
(anonymous) @ dojo.js:622
k @ dojo.js:431
(anonymous) @ dojo.js:622
t @ dojo.js:682
(anonymous) @ dojo.js:682
k @ dojo.js:431
r @ dojo.js:682
createProjectFolder @ main.js:89
(anonymous) @ main.js:74
Show 4 more frames
/ago/#:1 Access to fetch at 'https://www.arcgis.com/sharing/rest/content/users/myuser/createFolder' from origin 'https://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

Can anyone shed some light on what I have to do to get this to work?

request | ArcGIS API for JavaScript 4.14 

Create Folder—ArcGIS REST API: Users, groups, and content | ArcGIS for Developers  

0 Kudos
1 Solution

Accepted Solutions
JamesMoloney2
New Contributor II

SOLVED IT!

This is for anyone else that may be spending the next 24hrs trying to work this out....

function createProjectFolder(folderName) {
        var url = "https://www.arcgis.com/sharing/rest/content/users/yournameduserhere/createFolder";
        var folderParams = {
            folderName: folderName,
            title: folderName,
            f: "json",
            token: token
        };
        var encodedfolderParams = JSON_to_URLEncoded(folderParams);
        console.log(encodedfolderParams);

        var headers = {
            "Content-Type": "application/x-www-form-urlencoded"
        };

        esriRequest(url, {
            method: "POST",
            headers: headers,
            body: encodedfolderParams,
            responseType: "json",
        }).then(function (response) {
            // The requested data
            var geoJson = response.data;
            console.log(response);
        });

    };

you're welcome!

View solution in original post

0 Kudos
1 Reply
JamesMoloney2
New Contributor II

SOLVED IT!

This is for anyone else that may be spending the next 24hrs trying to work this out....

function createProjectFolder(folderName) {
        var url = "https://www.arcgis.com/sharing/rest/content/users/yournameduserhere/createFolder";
        var folderParams = {
            folderName: folderName,
            title: folderName,
            f: "json",
            token: token
        };
        var encodedfolderParams = JSON_to_URLEncoded(folderParams);
        console.log(encodedfolderParams);

        var headers = {
            "Content-Type": "application/x-www-form-urlencoded"
        };

        esriRequest(url, {
            method: "POST",
            headers: headers,
            body: encodedfolderParams,
            responseType: "json",
        }).then(function (response) {
            // The requested data
            var geoJson = response.data;
            console.log(response);
        });

    };

you're welcome!

0 Kudos