1

We are using SP2016 on-premise. In order to handle security test I prepared a PowerShell script to test upload with PUT/POST.

$Digest = $response.getcontextwebinformation.FormDigestValue $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $Headers.Add("accept","application/json;odata=verbose") $Headers.Add("X-RequestDigest",$Digest) Invoke-WebRequest -method Post -UseDefaultCredentials -uri $url -InFile $uploadPath -headers $Headers 

By using above script I can upload my file successfully. Return status code is 200.

Then I change the -method to PUT. The file STILL upload successfully. However the status code return is

Remote server returned an error: (400) Bad Request

Is it normal SharePoint behavior? Why SharePoint allow file upload while returning 400 error? Is it possible we can disallow PUT on SharePoint?

3
  • (Old thread!) I don't think you have to provide a X-RequestDigest header when calling sharepoint api from outside the http web application. Commented Dec 9, 2021 at 9:38
  • @SteveB sorry I don't have a chance to test. But my point is, if SharePoint allow me upload the file, why it return 400 error? Commented Dec 12, 2021 at 3:26
  • I can't reproduce your issue. Whether I provide digest or not, I can successfully upload a file, without any trouble. To narrow the diagnostic, can you describe how you build the url, and what is the body of the response ? Commented Dec 13, 2021 at 9:03

1 Answer 1

2

If it helps, here's a working example of file upload:

$siteColl = "http://mysharepoint/sites/mycoll" $targetFolder = "MyLib" $fileToUpload = "D:\path\to\mydocument.docx" $creds = if(-not $creds) { Get-Credential } else { $creds } Invoke-WebRequest -Uri "$sitecoll/$targetFolder/mydocument.docx" ` -InFile $fileToUpload ` -Credential $creds ` -Method Put 

This results in 200 return code

StatusCode : 200 StatusDescription : OK Content : {} RawContent : HTTP/1.1 200 OK X-SharePointHealthScore: 0 ResourceTag: rt:6E1D7470-0B2E-4332-BE74-C2B16B386DFA@00000000004 Public-Extension: http://schemas.microsoft.com/repl-2 SPRequestGuid: 5c530ca0-3ea5-7036-... Headers : {[X-SharePointHealthScore, 0], [ResourceTag, rt:6E1D7470-0B2E-4332-BE74-C2B16B386DFA@00000000004], [Public-Extension, http://schemas.microsoft.com/repl-2], [SPRequestGuid, 5c530ca0-3ea5-7036-e490-00894a0829ce]...} RawContentLength : 0 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.