POST requests with JSON body for MS SharePoint operations

Dec 13, 2014 at 9:23 AM
I use REST SDK to work with SharePoint 2013. To make some operations like creating or changing folders I need POST requests. It appears that those which contain JSON body do not work (server always responses HTTP 400 Bad request) while POST-s without body work well.
Can someone confirm whether I form request properly:
http_client_config config;
config.set_credentials(credentials(U("username"), U("password")));
http_client client(U(""), config);

http_request request;
request.headers().add(U("accept"), U("application/json; odata=verbose"));

// sharepoint-related stuff
request.headers().add(U("X-RequestDigest"), my_digest);
request.headers().add(U("X-HTTP-Method"), U("MERGE"));

// this should create folder but it does not - response HTTP 400
request.set_request_uri(web::uri::encode_uri(U("/_api/Web/GetFolderByServerRelativeUrl('/Shared Documents')/Folders")));
request.set_body(U("{ '__metadata':{ 'type': 'SP.Folder' }, 'ServerRelativeUrl':'new folder created' }"), U("application/json; odata=verbose"));
// other requests with request.set_body(... are also responded as HTTP 400
// but this works fine
// request.set_request_uri(web::uri::encode_uri(U("/_api/Web/Folders/add('/Shared Documents/new folder created')")));

// wait, parse result etc.
Dec 17, 2014 at 1:51 AM
Hi fruler,

I don't have any actual experience with the SharePoint REST API so I'm not sure what the problem is. I don't know of any problems in the C++ Rest SDK with setting JSON as the request body. In your particular case here you are setting the body as a string that you have formulated a JSON object with. At first glance one problem I see is you are using single quotes for strings in your JSON object. This is incorrect and not valid, you need to use double quotes.

If that doesn't fix the problem you also could try using a debugging proxy like Fiddler to make sure the request is going out as you expected.

Marked as answer by fruler on 12/16/2014 at 10:56 PM
Dec 17, 2014 at 6:56 AM
Thank you for reply.

I've manage to make it work - I've added body size parameter. Though I didn't still get why doesn't it works without explicit string length detection.
Single quotes btw are seems like OK for SharePoint (heh, Microsoft always supports things which go out of standard).
Marked as answer by fruler on 12/16/2014 at 10:56 PM