2

I am trying to upload an Attachment (file image in jpg format) to a table of SharePoint 2013 throught an Android POST Request.

That table has different columns and one of them is of type Attachment (with the clip icon). However, on the body of the request, I am not being able to upload any data to AttachmentFiles because I don't know how to do it (what is the format of the AttachmentFile I should send?). The code of my request and response is the following:

12-07 12:44:38.450 6133-7240/testapp.com.testapp D/OkHttp: --> POST https://urltest.es/iot/_api/web/lists/GetByTitle('RESULTS_COMMENTS')/items?$orderby=Signed%20asc&$top=100000 http/1.1
12-07 12:44:38.450 6133-7240/testapp.com.testapp D/OkHttp: {"AttachmentFiles":"/storage/emulated/0/Pictures/Attachment20171207_124428-1813646694.jpg","FORMId":3,"COMMENT":"mejdkd","__metadata":{"type":"SP.Data.RESULTS_x005f_COMMENTSListItem"},"USER_LOGIN":"testuser"}
12-07 12:44:38.500 6133-7240/testapp.com.testapp D/OkHttp: <-- 400 Bad Request https://urltest.es/iot/_api/web/lists/GetByTitle('RESULTS_COMMENTS')/items?$orderby=Signed%20asc&$top=100000 (52ms)
12-07 12:44:38.500 6133-7240/testapp.com.testapp D/OkHttp: {"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A 'PrimitiveValue' node with non-null value was found when trying to read the value of a navigation property; however, a 'StartArray' node, a 'StartObject' node, or a 'PrimitiveValue' node with null value was expected."}}}

If I remove the "AttachmentFiles" element from the request the petition works correctly and the item is created (201) in the table (but without Attachment).

I know that I am only passing the file path, but I tried to send the file in Base64 or as a byte array and it did not work either. Anyone knows how can the upload be done?

Thanks in advance.

UPDATE!

I tried to Attach the file in a separated call, as suggested and now the response from the server is 200 (OK), but when I try to recover the file, the server returns nothing (0x0 image in the GET request). The log from the new call is the following:

D: --> POST https://test.com.es/sites/iot/_api/web/lists/GetByTitle('RESULTS')/items(118)/AttachmentFiles/add(FileName='Attachment20171227_115411-1298092432.jpg') http/1.1
D: Content-Type: application/json;odata=verbose
D: Content-Length: 959012
D: X-RequestDigest: 0xBF892F87A74FD3F28E7FE5E8FAA970387C9B33DB05F99C61CC23553E07D394202CB20CBB1B9C0B81272539B72378010FE532192C516AA9F697CF64D8CE464454,27 Dec 2017 10:59:13 -0000
D: Accept: application/json;odata=verbose
D: [-1,-40,-1,-31,14,-12,69,120,105,102,0,0,73,73,42,0,8,0,0,0,13,0,0,1,4,0,1,0,0,0,0,8,0,0,1,1,4,0,1,0,0,0,-128,4,0,0,15,1,2,0,8,0,0,0,-86,0,0,0,16,1,2,0,8,0,0,0,-78,0,0,0,18,1,3,0,1,0,0,0,1,0,0,0,26,1,5,0,1,0,0,0,-70,0,0,0,27,1,5,0,1,0,0,0,-62,0,0,0,40,1,3,0,1,0,0,0,2,0,0,0,49,1,2,0,13,0,0,0,-54,0,0,0,50,1,2,0,20,0,0,0,-40,0,0,0,19,2,3,0,1,0,0,0,1,0,0,0,105,-121,4,0,1,0,0,0,-20,0,0,0,37,-120,4,0,1,0,0,0,8,3,0,0,26,3,0,0,83,65,77,83,85,78,71,0,83,77,45,84,51,54,53,0,72,0,0,0,1,0,0,0,72,0,0,0,1,0,0,0,84,51,54,53,88,88,85,49,66,80,75,50,0,0,50,48,49,55,58,49,50,58,50,55,32,49,49,58,53,52,58,49,54,0,25,0,-102,-126,5,0,1,0,0,0,30,2,0,0,-99,-126,5,0,1,0,0,0,38,2,0,0,34,-120,3,0,1,0,0,0,2,0,0,0,39,-120,3,0,1,0,0,0,-112,1,0,0,0,-112,7,0,4,0,0,0,48,50,50,48,3,-112,2,0,20,0,0,0,46,2,0,0,4,-112,2,0,20,0,0,0,66,2,0,0,1,-111,7,0,4,0,0,0,1,2,3,0,5,-110,5,0,1,0,0,0,86,2,0,0,7,-110,3,0,1,0,0,0,2,0,0,0,8,-110,3,0,1,0,0,0,0,0,0,0,9,-110,3,0,1,0,0,0,0,0,0,0,10,-110,5,0,1,0,0,0,94,2,0,0,124,-110,7,0,98,0,0,0,-120,2,0,0,0,-96,7,0,4,0,0,0,48,49,48,48,1,-96,3,0,1,0,0,0,1,0,0,0,2,-96,4,0,1,0,0,0,0,8,0,0,3,-96,4,0,1,0,0,0,-128,4,0,0,5,-96,4,0,1,0,0,0,-22,2,0,0,23,-94,3,0,1,0,0,0,2,0,0,0,1,-93,7,0,1,0,0,0,1,0,0,0,2,-92,3,0,1,0,0,0,0,0,0,0,3,-92,3,0,1,0,0,0,0,0,0,0,6,-92,3,0,1,0,0,0,0,0,0,0,32,-92,2,0,33,0,0,0,102,2,0,0,0,0,0,0,1,0,0,0,8,0,0,0,4,1,0,0,100,0,0,0,50,48,49,55,58,49,50,58,50,55,32,49,49,58,53,52,58,49,54,0,50,48,49,55,58,49,50,58,50,55,32,49,49,58,53,52,58,49,54,0,20,1,0,0,100,0,0,0,23,1,0,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,1,0,7,0,4,0,0,0,48,49,48,48,2,0,4,0,1,0,0,0,0,32,1,0,12,0,4,0,1,0,0,0,0,0,0,0,16,0,5,0,1,0,0,0,90,0,0,0,64,0,4,0,1,0,0,0,0,0,0,0,80,0,4,0,1,0,0,0,1,0,0,0,0,1,3,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,1,0,2,0,4,0,0,0,82,57,56,0,2,0,7,0,4,0,0,0,48,49,48,48,0,0,0,0,1,0,0,0,1,0,4,0,0,0,2,2,0,0,0,0,0,0,9,0,0,1,4,0,1,0,0,0,0,2,0,0,1,1,4,0,1,0,0,0,32,1,0,0,3,1,3,0,1,0,0,0,6,0,0,0,18,1,3,0,1,0,0,0,1,0,0,0,26,1,5,0,1,0,0,0,-116,3,0,0,27,1,5,0,1,0,0,0,-108,3,0,0,40,1,3,0,1,0,0,0,2,0,0,0,1,2,4,0,1,0,0,0,-100,3,0,0,2,2,4,0,1,0,0,0,80,11,0,0,0,0,0,0,72,0,0,0,1,0,0,0,72,0,0,0,1,0,0,0,-1,-40,-1,-37,0,-124,0,23,16,17,20,17,14,23,20,19,20,26,24,23,27,34,57,37,34,31,31,34,70,50,53,41,57,83,73,87,86,81,73,80,79,91,103,-125,111,91,97,124,99,79,80,114,-100,116,124,-120,-116,-109,-107,-109,89,110,-95,-83,-96,-113,-85,-125,-112,-109,-115,1,24,26,26,34,30,34,67,37,37,67,-115,94,80,94,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-115,-1,-64,0,17,8,1,32,2,0,3,1,34,0,2,17,1,3,17,1,-1,-60,1,-94,0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,16,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125,1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,-127,-111,-95,8,35,66,-79,-63,21,82,-47,-16,36,51,98,114,-126,9,10,22,23,24,25,26,37,38,39,40,41,42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-71,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-31,-30,-29,-28,-27,-26,-25,-24,-23,-22,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,1,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,17,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119,0,1,2,3,17,4,5,33,49,6,18,65,81,7,97,113,19,34,50,-127,8,20,66,-111,-95,-79,-63,9,35,51,82,-16,21,98,114,-47,10,22,36,52,-31,37,-15,23,24,25,26,38,39,40,41,42,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-126,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-7
D: 1,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-30,-29,-28,-27,-26,-25,-24,-23,-22,-14,-13,-12,-11,-10,-9,-8,-7,-6,-1,-38,0,12,3,1,0,2,17,3,17,0,63,0,-27,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,
D: -118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,
D: -94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,40,-94,-118,0,-1,-39,-1,-37,0,-124,0,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,3,2,2,2,2,2,4,3,3,2,3,5,4,5,5,5,4,4,4,5,6,7,6,5,5,7,6,4,4,6,9,6,7,8,8,8,8,8,5,6,9,10,9,8,10,7,8,8,8,1,1,1,1,2,2,2,4,2,2,4,8,5,4,5,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,-1,-64,0,17,8,4,-128,8,0,3,1,34,0,2,17,1,3,17,1,-1,-60,1,-94,0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,16,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125,1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,-127,-111,-95,8,35,66,-79,-63,21,82,-47,-16,36,51,98,114,-126,9,10,22,23,24,25,26,37,38,39,40,41,42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-71,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-31,-30,-29,-28,-27,-26,-25,-24,-23,-22,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,1,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,17,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119,0,1,2,3,17,4,5,33,49,6,18,65,81,7,97,113,19,34,50,-127,8,20,66,-111,-95,-79,-63,9,35,51,82,-16,21,98,114,-47,10,22,36,52,-31,37,-15,23,24,25,26,38,39,40,41,42,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-126,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-71,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-30,-29,-28,-27,-26,-25,-24,-23,-22,-14,-13,-12,-11,-10,-9,-8,-7,-6,-1,-38,0,12,3,1,0,2,17,3,17,0,63,0,-2,12,-103,-106,48,100,39,44,58,123,-44,112,43,73,-106,97,-64,-28,15,74,-120,43,101,92,-74,-12,-49,79,90,-77,-106,18,40,64,112,78,15,-75,0,69,41,-111,-127,-63,24,3,-91,58,33,-71,48,-53,-116,117,-55,-21,-17,82,-55,2,-86,-105,13,-72,-98,72,21,18,13,-49,-110,112,-125,-88,-51,105,-54,-102,1,-78,-60,-64,-4,-65,112,-14,78,122,82,-84,114,18,54,-126,-15,103,-65,106,-98,-31,89,-41,41,38,16,-13,-45,-83,42,-18,-122,52,102,99,-77,-81,38,-77,122,104,3,-60,74,-55,-67,31,-112,57,-56,-23,84,-98,85,117,100,-5,-104,56,27,-115,75,114,-93,102,-11,-56,82,50,49,-34,-88,-58,55,58,-126,51,-98,-108,1,42,-112,-116,62,124,-127,-63,-26,-99,35,9,9,-38,54,-29,-100,-98,-11,17,93,-116,1,-11,-32,-44,-62,55,39,111,0,30,-124,-98,-76,0,-127,23,-27,-63,5,-121,115,72,99,27,-55,110,112,51,-113,90,112,-73,-112,74,7,92,-98,-75,59,91,21,-112,-68,-121,11,-114,-108,1,81,88,56,-38,125,112,9,-87,-104,44,15,-61,99,35,32,83,-92,-122,53,96,66,-19,-49,35,35,-83,71,38,10,46,87,-111,-23,64,19,-18,50,-123,103,114,72,56,25,61,-86,-92,-84,102,127,-112,101,71,0,-45,-74,-71,1,-70,-116,113,-51,68,55,35,-4,-65,-107,0,78,-111,20,82,-17,-48,-116,-113,106,72,-54,-69,28,110,32,-10,39,-91,57,-117,75,-75,27,-27,29,121,-17,77,49,-7,114,21,4,15,112,104,2,72,98,38,97,25,-56,82,121,3,-75,37,-28,80,-59,62,-56,-37,-127,-41,39,-67,47,-17,50,113,-115,-64,113,-118,-84,-22,73,36,-32,30,-89,38,-128,45,-108,-113,106,49,113,-57,35,29,-22,45,-8,73,50,112,65,-56,7,-46,-91,107,117,72,-105,115,29,-60,115,-98,-43,89,19,-51,-108,33,-32,99,-75,0,34,43,57,44,71,-54,6,9,52,-19,-63,89,118,12,-25,-118,-79,113,19,-96,41,31,-35,-57,34,-104,14,66,29,-127,112,113,-116,80,2,-77,-58,-50,-120,-21,-100,-11,52,-109,91,4,113,-77,-112,64,35,53,24,80,73,98,14,-29,-56,-55,-89,-7,-116,-103,102,5,-101,24,3,-46,-128,36,14,-84,-97,-68,-64,-63,-64,24,-90,72,99,80,-92,40,-25,-34,-107,68,102,9,36,35,107,-125,-64,-57,90,-81,-69,-52,93,-89,10,71,67,-21,64,18,-128,11,12,113,-21,-19,83,73,18,21,109,-83,-12,57,-86,-101,29,87,-27,-7,-78,122,-114,-43,56,97,-75,55,-15,-127,-64,-
D: 49,-21,64,21,-100,-2,-19,122,-25,60,-15,90,0,-96,-127,68,121,43,-112,121,-20,106,-77,-93,-74,20,0,20,-98,-44,-93,112,79,40,2,72,57,-29,-75,0,70,-51,-105,42,49,-100,-32,-44,-47,66,25,11,-109,-116,19,-63,-89,-58,-125,118,88,99,-116,28,-118,-123,-25,-38,-52,-120,50,9,-17,64,18,44,113,-78,-79,57,12,7,76,-43,29,-66,-100,-100,-12,-57,90,-99,-101,37,64,-62,-25,-126,79,122,66,-31,126,-23,12,65,-57,34,-128,36,-14,-105,-54,44,120,-57,99,-38,-102,-116,89,66,15,-70,79,56,29,105,85,-39,-43,85,-113,83,-64,3,-83,57,-99,81,-16,21,84,-11,-6,80,4,74,28,-71,-14,-55,24,-32,-112,58,-45,-111,-128,44,101,57,62,-121,-67,73,28,-121,123,-116,14,-99,-121,90,102,-43,-35,-106,-5,-92,-13,-114,-44,0,-60,-109,-100,40,98,79,64,42,-53,-88,100,77,-57,107,30,125,-59,63,-55,68,27,-94,-32,-11,4,-98,-107,89,60,-58,-109,115,-18,-50,122,-102,0,87,98,-123,87,61,58,-32,117,-87,11,-61,-111,-123,-6,-29,-67,72,97,15,38,-3,-61,104,-32,-125,-38,-85,-54,-58,25,8,11,-112,78,70,123,-48,4,-101,119,47,-18,-14,57,-4,-22,16,-62,41,50,-5,-117,103,28,118,-89,-93,-77,99,39,103,57,-64,-17,78,45,-119,126,101,-56,35,-126,104,-80,16,-68,-69,-97,57,-58,120,-50,105,-56,98,66,14,114,115,-55,52,-42,80,-46,49,-58,59,-114,58,-48,80,22,92,-12,61,-3,104,1,93,99,71,44,-92,17,-116,-116,-114,-76,7,82,124,-62,57,3,24,21,12,-121,-26,96,56,0,-32,15,74,124,101,66,18,-39,-21,-126,113,-46,-115,-74,2,68,59,-61,-29,-114,115,-125,77,117,37,-63,98,74,-114,-98,-43,41,-115,91,12,-92,21,-57,110,-43,3,-85,33,-21,-100,-10,-12,-96,9,124,-91,-28,-77,109,-56,-29,53,2,-95,-36,70,125,-78,59,-44,-83,-9,1,-2,60,126,116,70,-121,60,-74,59,-48,3,35,-5,-81,-23,-21,79,120,-48,13,-22,-8,108,100,10,-107,-93,46,-37,64,-38,-89,-112,125,106,-77,67,32,102,24,60,117,38,-128,35,82,67,103,-97,-62,-89,18,40,87,-7,115,-111,-126,79,106,-118,49,-55,-10,-21,-19,79,8,89,9,-32,0,113,-49,106,0,100,105,-26,54,63,95,74,122,-88,-13,2,6,24,-50,9,35,-91,73,26,-108,-34,55,116,25,-23,77,68,85,-109,-26,-55,66,51,-102,0,89,2,38,87,-82,122,31,74,-127,113,-45,4,-28,-15,-114,-11,97,-54,41,-7,121,0,-28,19,-34,-109,-121,27,-108,17,-49,52,0,-101,7,32,-74,-46,6,64,29,-22,24,-48,-69,0,-71,-11,-29,-75,72,20,17,-110,114,65,-91,-53,41,96,-99,79,4,-114,-44,1,98,53,116,66,91,28,-16,13,66,85,70,-47,-98,-121,-98,105,83,98,-97,-103,-40,2,59,-44,77,27,22,37,121,29,65,52,1,62,-63,-55,-37,-58,51,-41,-83,87,64,12,-104,98,113,-100,84,-118,36,-124,-114,9,4,113,-125,72,-88,-37,-73,-74,65,7,52,0,-10,-115,68,-125,-26,24,35,3,61,-87,14,-60,86,92,-125,-20,41,-114,-114,-51,-109,-116,-102,104,-113,61,-2,-76,-46,-69,-80,15,-124,49,-49,36,33,-29,25,-95,-58,1,-25,-114,-89,21,32,68,78,-67,-24,-33,-75,-126,-72,-54,-98,-122,-101,-117,91,-127,25,93,-85,-14,-100,-125,-33,20,-59,125,-84,9,94,65,-28,-43,-110,-103,44,-96,97,0,-56,34,-104,-37,72,-64,4,96,96..............]
D: --> END POST (959012-byte body)
I: Background partial concurrent mark sweep GC freed 128360(7MB) AllocSpace objects, 2(14MB) LOS objects, 40% free, 15MB/26MB, paused 1.906ms total 151.953ms
D: <-- 200 OK https://test.com.es/sites/iot/_api/web/lists/GetByTitle('RESULTS')/items(118)/AttachmentFiles/add(FileName='Attachment20171227_115411-1298092432.jpg') (1035ms)
D: Cache-Control: private, max-age=0
D: Transfer-Encoding: chunked
D: Content-Type: application/json;odata=verbose;charset=utf-8
D: Expires: Tue, 12 Dec 2017 11:00:18 GMT
D: Last-Modified: Wed, 27 Dec 2017 11:00:18 GMT
D: Vary: Accept-Encoding
D: Server: Microsoft-IIS/8.5
D: X-SharePointHealthScore: 0
D: SPClientServiceRequestDuration: 883
D: X-AspNet-Version: 4.0.30319
D: SPRequestGuid: e29c3a9e-e928-f0f7-70ed-205970d9766b
D: request-id: e29c3a9e-e928-f0f7-70ed-205970d9766b
D: X-RequestDigest: 0x6805AE58194C565F443FCEE1A7A119F1D0B46E55CEA73B39BACBF80F86BBD06BB2FE53B91A71D5653266B5C5409612CE80460CE4CC5DBD3A73119B684A7AD56C,27 Dec 2017 11:00:18 -0000
D: X-FRAME-OPTIONS: SAMEORIGIN
D: X-Powered-By: ASP.NET
D: MicrosoftSharePointTeamServices: 15.0.0.4569
D: X-Content-Type-Options: nosniff
D: X-MS-InvokeApp: 1; RequireReadOnly
D: Date: Wed, 27 Dec 2017 11:00:19 GMT
D: {"d":{"__metadata":{"id":"Web/Lists(guid'1be125bd-2557-4298-97c2-5d55ca3a9f49')/Items(118)/AttachmentFiles('Attachment20171227_115411-1298092432.jpg')","uri":"https://test.com.es/sites/iot/_api/Web/Lists(guid'1be125bd-2557-4298-97c2-5d55ca3a9f49')/Items(118)/AttachmentFiles('Attachment20171227_115411-1298092432.jpg')","type":"SP.Attachment"},"FileName":"Attachment20171227_115411-1298092432.jpg","ServerRelativeUrl":"/sites/iot/Lists/RESULTS/Attachments/118/Attachment20171227_115411-1298092432.jpg"}}
D: <-- END HTTP (548-byte body)

UPDATE 2: I tried to generate the bytearray as following:

Bitmap bitmap = BitmapFactory.decodeFile(
        bosCommentResult.getAttachmentFile().getAbsolutePath());

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 2, stream);
byte[] byteArray = stream.toByteArray();

But the result is the same, when I try to see the image file from the direct link generated BASE_URL/RESULTS/Attachments/idattachment/Attachment.png it contains errors and I cannot see anything. (Note that if I print the Bitmap inside an ImageView the image shows correctly).

3
  • 1
    You should consider posting your solution in an Answer post instead of your question. This way, it will be easier for others to find it. Commented Jan 3, 2018 at 14:28
  • @user3429953 please add a separate answer so I can award the bounty to you. Commented Jan 4, 2018 at 5:27
  • Thanks both of you. I created an answer with all the details of the solution. Hope it help future Android developers!! :D
    – raviolillo
    Commented Jan 4, 2018 at 14:42

3 Answers 3

3

Following is jQuery example of attaching file via REST

<input type="file" id="fileUpload1" />
<input type="submit" id="buttonClick" onClick="buttonSubmitClick()" />

<script type="text/javascript">
function buttonSubmitClick() {

    event.stopPropagation(); // Stop stuff happening
    event.preventDefault(); // Totally stop stuff happening

     var file = $('#fileUpload1')[0].files[0];

      var getFileBuffer = function(file) {

       var deferred = $.Deferred();
       var reader = new FileReader();

       reader.onload = function(e) {
        deferred.resolve(e.target.result);
       }

       reader.onerror = function(e) {
        deferred.reject(e.target.error);
       }

       reader.readAsArrayBuffer(file);

       return deferred.promise();
      };

       getFileBuffer(file).then(function(buffer) {

       $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle(" + listName + ")/items(" + itemId + ")/AttachmentFiles/add(FileName='" + file.name + "')",
        type: 'POST',
        cache: false,
        contentType: false, // Set content type to false as jQuery will tell the server its a query string request
        method: 'POST',
        data: buffer,
        processData: false,
        headers: {
         "Accept": "application/json; odata=verbose",
         "content-type": "application/json; odata=verbose",
         "X-RequestDigest": $('#__REQUESTDIGEST').val(),
        },
        success: function(data, textStatus, jqXHR)
        {
            if(typeof data.error === 'undefined')
            {
                console.log("success");
            }
            else
            {
                // Handle errors here
                console.log('ERRORS: ' + data.error);
            }
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            // Handle errors here
            console.log('ERRORS: ' + textStatus);
            // STOP LOADING SPINNER
        }
       });

      }); 
  }

  </script>

So in short you need to define only File Name in the request URL and than headers will contain the file type and file contents.

You will first create the item and using it's newly generated ID, again make a request to attach file.

UPDATE

Above I have added a working example which I added as in SharePoint page using Content Editor Web Part. Few changes here and there and it worked.

Below is the result when I log the buffer:

Array Buffer

19
  • 1
    OK, got it. Thanks for the quick response! What a shame it cannot be done in the same POST than the other elements of the item :(
    – raviolillo
    Commented Dec 7, 2017 at 14:10
  • I tried to do it, but I still cannot find the way to establish the buffer in Java. What kind of object has it to be? Thanks!
    – raviolillo
    Commented Dec 20, 2017 at 15:24
  • @user3429953 I have added the function to get the file buffer Commented Dec 21, 2017 at 5:56
  • 1
    I'm sorry brother, please untick my answer.. and I will create a bounty on your question for others to answer Commented Dec 27, 2017 at 11:28
  • 2
    Look I found this: stackoverflow.com/questions/11766878/…
    – Marco
    Commented Dec 27, 2017 at 21:00
1

OK! After a lot of tries I finally could upload an attachment image file. The first thing to notice is that the attachment cannot be uploaded at the same time than the other info of the POST, so it's a completely separated POST request (as @arsalan-adam-khatri suggested) using the ID that the SharePoint returns after creating the new row in the table.

The new call should be similar to:

https://BASE_URL/_api/web/lists/GetByTitle('TABLE_NAME')/items(ROW_ID_NUMBER)/AttachmentFiles/add(FileName='NAME_OF_ATTACHMENT')

Note that it also to be added the following header:

"X-RequestDigest": digestValue

Then, the final thing to do is create the body of the POST, which should be the file byte buffer to be attached. This is the most complex part of the procedure, since I tried a lot of things that did not work:

  • Image file as File
  • Image encoded in Base64 as String
  • Image as byte array (byte[])
  • Image as BufferedOutputStream
  • Image as Input/Output Stream

And a lot of other similar solutions that none of them work.

BUT finally I found a solution. The first thing to notice about the solution is that I am using the dependencies of Retrofit and OkHttp3 in order to generate the API Service, the SharePoint calls, intercept the responses, etc. Taking this into account, the body of the request should be a RequestBody object (given by the OkHttp3 dependency), created as you can see in the code below:

InputStream in = new FileInputStream(file);
byte[] buf;
buf = new byte[in.available()];
while (in.read(buf) != -1) {

}
// Note that creating the image as MultipartBody.Part does not work. 
// Also does not work to create the RequestBody with MediaType.parse("image/*")
RequestBody requestBody = RequestBody
       .create(MediaType.parse("application/octet-stream"), buf);

And then, the call to the SharePoint (with Retrofit) is something like:

@POST(ApiUrls.Endpoint.UPLOAD_IMAGE_ATTACHMENT)
Observable<Void> uploadImageAttachment(
        @Header(ApiUrls.Param.X_REQUEST_DIGEST) String digestValue,
        @Path(ApiUrls.Param.RESULT_ID) int resultId,
        @Path(ApiUrls.Param.FILE_NAME) String fileName,
        @Body RequestBody attachment);

Where:

ApiUrls.Endpoint.UPLOAD_IMAGE_ATTACHMENT = The URL endpoint which is a constant value (for example, "https://test.com/es/_api/web/lists/GetByTitle('COMMENTS')/items('{resultId}')/AttachmentFiles/add(FileName='{filename}')")
resultID = The ROW_ID_NUMBER (for example, 108)
filename = The NAME_OF_ATTACHMENT (for example, "attach.png")
attachment = The requestBody just created

Hope it helps!!

PD: I do not know how to do this without the Retrofit and OkHttp3 dependencies (that are very commonly used), so any extensions of this solution without using them will be grateful.

1

For people who are reading in 2023, simple answer is just don't use multipart upload. SharePoint gets confused with multipart uploads. use simple upload if using retrofit with okhttp3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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