I'm attempting to create an item in SharePoint on premise 2016 list using Java and SharePoint API. Forgive me if I missed it - I see a few related questions but they do not seem to really answer my question.
I'm able to do GET and fetch digest value. I am getting error code 400 when attempting to create a list item.
Questions:
- Do I still need to worry about authentication? As added complication, I'm attempting to do this using a chatbot attached to internal company messaging system. The sharepoint list is also internal and I never really had to worry about authentication when doing GET or fetching digest value. I'm hoping not because the code is 400, as opposed to 401 or 403
- If I do not - from what I've read error 400 could be caused because of incorrect data format to be written into the list or some Java peculiarities with respect to setting Content-Length for output stream. Hoping to find clarity on this.
This is my data:
data = {"__metadata":{"type":"SP.Data.MyListTitleListItem"},"Title ":"New Title"}
My code:
URL url = new URL("https://[SharePoint site]/_api/web/lists/GetByTitle('MyListTitle')/items?
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Accept", "application/json;odata=verbose");
urlConnection.setRequestProperty("POST");
urlConnection.setRequestProperty("Content-Type", "application/json;odata=verbose");
urlConnection.setRequestProperty("X-RequestDigest", formDigestValue);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
OutputStream os = urlConnection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write(data);
urlConnection.connect();