AnsweredAssumed Answered

Code 400: Bad Request while uploading a file in Sharepoint library

Question asked by kuladeep.surisetti841973 on Feb 20, 2018
Latest reply on May 10, 2018 by gouthamr388096

I am trying to upload a file to a document library and keep getting a statusCode of 400 with the text just saying "Bad Request." I was rifling through some other forums and saw that the API could potentially not have the right permissions but I haven't been able to figure out how to even check that, let alone set it to have full control.

Additionally, the site where the document library lives is a subsite, so I'm not sure if that has an effect on what I'm trying to do but I haven't seen an example where the library lives in a subsite to know if it does affect anything.


sharepoint folder url:-

https://share.XYZ.net/sites/ABC/DEF/Shared%20Documents/Forms/AllItems.aspx


REST API being tried:-

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


API for accessToken :-

https://accounts.accesscontrol.windows.net/f35a69XY-607f-47d4-82d7-ff31d7dc53a5/tokens/OAuth/2


using ClientID,ClientSecret, Redirect_URI, ResourceID in query parameters.

API for RequestDigest:-


https://share.XYZ.net/sites/ABC/DEF/_api/contextinfo

Accept : application/json ;odata=verbose
Authorization: "Bearer " + accessToken

File upload Call:-


https://share.XYZ.net/sites/ABC/DEF/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')/Files/add(url='a.txt',overwrite=true)

Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
content-length:length of post body


Error Recieved:-

Parameter 1 value = Error message received from Http Server, Code 400: Bad Request

 

Please let me know if you have any suggestions??

Outcomes