Skip to main content
Tweeted twitter.com/StackSharePoint/status/861171684744802305
added 278 characters in body
Source Link
Chetvergov
  • 613
  • 1
  • 9
  • 20

withThanks to odata=nometadata still having 415 error, but request payload has changedwjervis here is working example:

------WebKitFormBoundaryMvghBwmatyVLErkw Content-Disposition: form-data; name="json"

{"Title":"newItemTitle"} ------WebKitFormBoundaryMvghBwmatyVLErkw--`

To make it looks exactly like ajax-like Content-Disposition parameter has to be removed I suppose...

var item = {
    'Title': 'newItemTitle'
};


return fetch(_spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", {
    method: "POST",
    credentials: 'same-origin',
    body: JSON.stringify(item),
    headers: { 
        "Accept": "application/json; odata=nometadata",
        "Content-Type": "application/json;odata=nometadata",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()            
    }
})

with odata=nometadata still having 415 error, but request payload has changed:

------WebKitFormBoundaryMvghBwmatyVLErkw Content-Disposition: form-data; name="json"

{"Title":"newItemTitle"} ------WebKitFormBoundaryMvghBwmatyVLErkw--`

To make it looks exactly like ajax-like Content-Disposition parameter has to be removed I suppose...

Thanks to wjervis here is working example:

var item = {
    'Title': 'newItemTitle'
};


return fetch(_spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", {
    method: "POST",
    credentials: 'same-origin',
    body: JSON.stringify(item),
    headers: { 
        "Accept": "application/json; odata=nometadata",
        "Content-Type": "application/json;odata=nometadata",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()            
    }
})
formatting
Source Link
wjervis
  • 5.7k
  • 25
  • 45
------WebKitFormBoundaryFSelaakwmV2hvl6e
Content-Disposition: form-data; name="__metadata"

{"type":"SP.Data.MyListItem"}
------WebKitFormBoundaryFSelaakwmV2hvl6e
Content-Disposition: form-data; name="Title"

newItemTitle
------WebKitFormBoundaryFSelaakwmV2hvl6e--

------WebKitFormBoundaryFSelaakwmV2hvl6e Content-Disposition: form-data; name="__metadata"

{"type":"SP.Data.MyListItem"} ------WebKitFormBoundaryFSelaakwmV2hvl6e Content-Disposition: form-data; name="Title"

newItemTitle ------WebKitFormBoundaryFSelaakwmV2hvl6e--

`------WebKitFormBoundaryMvghBwmatyVLErkw Content-Disposition: form-data; name="json"

------WebKitFormBoundaryMvghBwmatyVLErkw Content-Disposition: form-data; name="json"

{"Title":"newItemTitle"} ------WebKitFormBoundaryMvghBwmatyVLErkw--`

{"Title":"newItemTitle"} ------WebKitFormBoundaryMvghBwmatyVLErkw--`

------WebKitFormBoundaryFSelaakwmV2hvl6e
Content-Disposition: form-data; name="__metadata"

{"type":"SP.Data.MyListItem"}
------WebKitFormBoundaryFSelaakwmV2hvl6e
Content-Disposition: form-data; name="Title"

newItemTitle
------WebKitFormBoundaryFSelaakwmV2hvl6e--

`------WebKitFormBoundaryMvghBwmatyVLErkw Content-Disposition: form-data; name="json"

{"Title":"newItemTitle"} ------WebKitFormBoundaryMvghBwmatyVLErkw--`

------WebKitFormBoundaryFSelaakwmV2hvl6e Content-Disposition: form-data; name="__metadata"

{"type":"SP.Data.MyListItem"} ------WebKitFormBoundaryFSelaakwmV2hvl6e Content-Disposition: form-data; name="Title"

newItemTitle ------WebKitFormBoundaryFSelaakwmV2hvl6e--

------WebKitFormBoundaryMvghBwmatyVLErkw Content-Disposition: form-data; name="json"

{"Title":"newItemTitle"} ------WebKitFormBoundaryMvghBwmatyVLErkw--`

added 400 characters in body
Source Link
Chetvergov
  • 613
  • 1
  • 9
  • 20

Trying to create list item:

let data = new FormData();
data.append("__metadata", JSON.stringify({ "type": itemType }));
data.append("Title", 'newItemTitle');

return fetch(_spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", {
    method: "POST",
    credentials: 'same-origin',
    body: data,
    headers: { 
        "Accept": "application/json; odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()            
    }
}).then(
        response => response.json())
        .then( 
            (r: any) => r.d.results);
}

Got following error: 415 Unsupported Media Type

In Request Payload:

------WebKitFormBoundaryFSelaakwmV2hvl6e
Content-Disposition: form-data; name="__metadata"

{"type":"SP.Data.MyListItem"}
------WebKitFormBoundaryFSelaakwmV2hvl6e
Content-Disposition: form-data; name="Title"

newItemTitle
------WebKitFormBoundaryFSelaakwmV2hvl6e--



Meanwhile JQuery ajax request works fine: 201 Created

var item1 = {
    "__metadata": { "type": itemType1 },
    "Title": 'newItemTitle'
};

$.ajax({
    url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
    type: "POST",
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(item1),
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
        var i = data;
    },
    error: function (data) {
        var i = data;
    }
});

Request Payload:

{"__metadata":{"type":"SP.Data.MyListItem"},"Title":"newItemTitle"}

Any ideas how Fetch could be used to create list item? I assume that Request Payload format has to be JQuery-like...



Update:

with odata=nometadata still having 415 error, but request payload has changed:

`------WebKitFormBoundaryMvghBwmatyVLErkw Content-Disposition: form-data; name="json"

{"Title":"newItemTitle"} ------WebKitFormBoundaryMvghBwmatyVLErkw--`

To make it looks exactly like ajax-like Content-Disposition parameter has to be removed I suppose...

Trying to create list item:

let data = new FormData();
data.append("__metadata", JSON.stringify({ "type": itemType }));
data.append("Title", 'newItemTitle');

return fetch(_spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", {
    method: "POST",
    credentials: 'same-origin',
    body: data,
    headers: { 
        "Accept": "application/json; odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()            
    }
}).then(
        response => response.json())
        .then( 
            (r: any) => r.d.results);
}

Got following error: 415 Unsupported Media Type

In Request Payload:

------WebKitFormBoundaryFSelaakwmV2hvl6e
Content-Disposition: form-data; name="__metadata"

{"type":"SP.Data.MyListItem"}
------WebKitFormBoundaryFSelaakwmV2hvl6e
Content-Disposition: form-data; name="Title"

newItemTitle
------WebKitFormBoundaryFSelaakwmV2hvl6e--



Meanwhile JQuery ajax request works fine: 201 Created

var item1 = {
    "__metadata": { "type": itemType1 },
    "Title": 'newItemTitle'
};

$.ajax({
    url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
    type: "POST",
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(item1),
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
        var i = data;
    },
    error: function (data) {
        var i = data;
    }
});

Request Payload:

{"__metadata":{"type":"SP.Data.MyListItem"},"Title":"newItemTitle"}

Any ideas how Fetch could be used to create list item? I assume that Request Payload format has to be JQuery-like...

Trying to create list item:

let data = new FormData();
data.append("__metadata", JSON.stringify({ "type": itemType }));
data.append("Title", 'newItemTitle');

return fetch(_spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", {
    method: "POST",
    credentials: 'same-origin',
    body: data,
    headers: { 
        "Accept": "application/json; odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()            
    }
}).then(
        response => response.json())
        .then( 
            (r: any) => r.d.results);
}

Got following error: 415 Unsupported Media Type

In Request Payload:

------WebKitFormBoundaryFSelaakwmV2hvl6e
Content-Disposition: form-data; name="__metadata"

{"type":"SP.Data.MyListItem"}
------WebKitFormBoundaryFSelaakwmV2hvl6e
Content-Disposition: form-data; name="Title"

newItemTitle
------WebKitFormBoundaryFSelaakwmV2hvl6e--



Meanwhile JQuery ajax request works fine: 201 Created

var item1 = {
    "__metadata": { "type": itemType1 },
    "Title": 'newItemTitle'
};

$.ajax({
    url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
    type: "POST",
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(item1),
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
        var i = data;
    },
    error: function (data) {
        var i = data;
    }
});

Request Payload:

{"__metadata":{"type":"SP.Data.MyListItem"},"Title":"newItemTitle"}

Any ideas how Fetch could be used to create list item? I assume that Request Payload format has to be JQuery-like...



Update:

with odata=nometadata still having 415 error, but request payload has changed:

`------WebKitFormBoundaryMvghBwmatyVLErkw Content-Disposition: form-data; name="json"

{"Title":"newItemTitle"} ------WebKitFormBoundaryMvghBwmatyVLErkw--`

To make it looks exactly like ajax-like Content-Disposition parameter has to be removed I suppose...

Source Link
Chetvergov
  • 613
  • 1
  • 9
  • 20
Loading