1

I have read some articles about adding a file to SharePoint where the body contains text when using a REST POST call with GetFolderByServerRelativeUrl body add().

Can anybody help me to add a pdf file this way. See below as an example I found while googling ?

What is the field to be used?

url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/add(url='a.txt',overwrite=true)
method: POST
body: "Contents of file"
Headers: 
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
content-length:length of post body
0

Hello you can refer below code to create a file in SharePoint using rest.

function createDocument() {
var docTitle = "testFile.pdf";
var docContent = "Test content";

var fullUrl = _spPageContextInfo.webAbsoluteUrl+ "/_api/web/GetFolderByServerRelativeUrl('Shared Documents')/Files/add(url='" + docTitle +"',overwrite=true)";

$.ajax({
url: fullUrl,
type: "POST",
data: docContent,
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest":$("#__REQUESTDIGEST").val()
},
success: function(){alert('pass')},
error: function(){alert('fail')}
});
}

Add you can call createDocument() function when you need to create a file. Reference

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.