This is the second are 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
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-
First see the XML return based on querying schema for Categories List using following URL
http://<Host Name>/_api/Web/Lists/getByTitle('Categories')
Then we will see the XML return based on querying for Categories List Items using following URL
http://<Host Name>/_api/Web/Lists/getByTitle('Categories')/Items
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”
Once we execute this request we can inspect the response to ensure that the request item has been added successfully to the Categories List.
Also we can validate this new item added by browsing Categories List
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" };
Once the request executed successfully we can see the item is updated in the Categories List
Delete List Item
http://<Host Name>/_api/Web/Lists/getByTitle('Categories')/Items(9)
Delete operation is more or less similar to Update operations.
In case of delete we will use of “X-Http-Method: DELETE” in the Request Body
Request Body
IF-MATCH: * X-Http-Method: DELETE
Once the request executed successfully we can validate the item is deleted from the list.
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" }
Once this request has been executed successfully we can see the Response Body holding information about newly added SharePoint List
Also we can see this new list added to SharePoint by browsing the respective site
Also we can verify the “AllowContentTypes” & “ContentTypesEnabled” properties are configured as expected by browsing the Advanced Properties of the new List as shown below-
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
Once the request has been completed, it will delete the required list from SharePoint Lists Collection.
Hope you find it helpful.