SharePoint Developer Tools: How To Test & Debug SharePoint Rest API Endpoints (Post Requests)
This is the second article in the series of using Fiddler as Debugging & Testing Tool for SharePoint REST API EndPoints.
You can read the article on GET Request here:
SHAREPOINT DEVELOPER TOOLS: HOW TO TEST & DEBUG SHAREPOINT REST API ENDPOINTS (GET REQUESTS)
POST requests are different in nature than GET requests. They require more authentication layers to get through in order to push the data to SharePoint Lists and Libraries.
In order to run the POST request successfully, we need an additional request header “X-RequestDigest” which is not but the User Authentication Token.
In order to request this token from SharePoint we need to make of “contextInfo” endpoint that will return the “FormDigestValue” containing the required user authentication token.
Now let see how we can request Authentication Token from SharePoint
Get Authorization Token
http://<Host Name>_api/contextinfo
https://howtodowithsharepoint.files.wordpress.com/2017/08/117.png?w=800
https://howtodowithsharepoint.files.wordpress.com/2017/08/24.png?w=800
Once we get the Authentication Token from SharePoint, we can add this token information in the Request Header of each of the POST requests
Request Headers
Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose
X-RequestDigest: 0xE1AE266A42214DA2940689826F68426D10620220CEDD3093CA2C234993E4ECA265BA57D357E8D3BD32F56660613CADBF72495F2C858B38F7C9B9C3CAD797F6D5,06 Feb 2017 01:22:08 -0000
Once we are ready with Request Headers we can start issuing POST Requests as shown below-
Add Data to List
Let’s consider we have a list called Categories as shown below-
https://howtodowithsharepoint.files.wordpress.com/2017/08/34.png?w=800
First, see the XML return based on querying schema for Categories List using following URL
http://<Host Name>/_api/Web/Lists/getByTitle('Categories')
https://howtodowithsharepoint.files.wordpress.com/2017/08/44.png?w=800
Then we will see the XML return based on querying for Categories List Items using the following URL
http://<Host Name>/_api/Web/Lists/getByTitle('Categories')/Items
https://howtodowithsharepoint.files.wordpress.com/2017/08/54.png?w=800
Next step is to prepare the Request Body and we have to include following properties to add the items.
Please note that I am taking properties that are required for this list to add the category and add any desired number of properties to the Request Body as per the schema of the target list.
Request Body
"__metadata": { type: " SP.Data.CategoriesListItem" },
Title: "Category From Fiddler",
CategoryID: 9,
Description: “New Category Added from Fiddler”
https://howtodowithsharepoint.files.wordpress.com/2017/08/64.png?w=800
Once we execute this request we can inspect the response to ensure that the requested item has been added successfully to the Categories List.
https://howtodowithsharepoint.files.wordpress.com/2017/08/74.png?w=800
Also, we can validate this new item added by browsing Categories List
https://howtodowithsharepoint.files.wordpress.com/2017/08/84.png?w=800
Update List Item
http://<Host Name>/_api/Web/Lists/getByTitle('Categories')/Items(9)
For update request you have to include “eTag” value that was returned with the item during the initial query to the Request Body. SharePoint uses this value to determine if there is any updates made to the item since it is last queried.
“If-Match: *” can be used to match any “eTag” value resulting in the operation being performed regardless of the actual value.
“X-Http-Method: PATCH” is to override the existing values
So the request body would be like this
IF-MATCH: *
X-Http-Method: PATCH
{
"__metadata": {
type: "SP.Data.CategoriesListItem"
},
Title: "Category From Fiddler - Updated",
Description: "New Category Added from Fiddler - Updated"
};
https://howtodowithsharepoint.files.wordpress.com/2017/08/94.png?w=800
Once the request executed successfully we can see the item is updated in the Categories List
https://howtodowithsharepoint.files.wordpress.com/2017/08/104.png?w=800
Delete List Item
http://<Host Name>/_api/Web/Lists/getByTitle('Categories')/Items(9)
Delete operation is more or less similar to Update operations.
https://howtodowithsharepoint.files.wordpress.com/2017/08/118.png?w=800
In case of delete we will use of “X-Http-Method: DELETE” in the Request Body
Request Body
IF-MATCH: *
X-Http-Method: DELETE
https://howtodowithsharepoint.files.wordpress.com/2017/08/124.png?w=800
Once the request executed successfully we can validate the item is deleted from the list.
https://howtodowithsharepoint.files.wordpress.com/2017/08/134.png?w=800
Add New List
http://<Host Name>/_api/Web/Lists
Adding a new SharePoint List involve a little bit more of configuration information in Request body apart from request headers
Request Headers
Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose
Request Body
Content-Length: 0
{
"__metadata": { type: "SP.List" },
"AllowContentTypes":true,
"ContentTypesEnabled":true,
"Description":"This is Task List Created using Fiddler",
"BaseTemplate": 107,
"Title": "Task List By Fiddler"
}
https://howtodowithsharepoint.files.wordpress.com/2017/08/144.png?w=800
Once this request has been executed successfully we can see the Response Body holding information about newly added SharePoint List
https://howtodowithsharepoint.files.wordpress.com/2017/08/154.png?w=800
Also, we can see this new list added to SharePoint by browsing the respective site
https://howtodowithsharepoint.files.wordpress.com/2017/08/162.png?w=800
https://howtodowithsharepoint.files.wordpress.com/2017/08/172.png?w=800
Also, we can verify the “AllowContentTypes” & “ContentTypesEnabled” properties are configured as expected by browsing the Advanced Properties of the new List as shown below-
https://howtodowithsharepoint.files.wordpress.com/2017/08/182.png?w=800
Delete List
http://<Host Name>/_api/Web/Lists/getByTitle('Task%20List%20By%20Fiddler')
Deleting a list is rather simpler than adding it. It takes “X-Http-Method: DELETE” to be added to the request header and rest will be done for you.
Request Headers
Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose
Content-Length: 0
IF-MATCH: *
X-Http-Method: DELETE
https://howtodowithsharepoint.files.wordpress.com/2017/08/192.png?w=800
Once the request has been completed, it will delete the required list from SharePoint Lists Collection.
https://howtodowithsharepoint.files.wordpress.com/2017/08/201.png?w=800
Hope you find it helpful.