[SalesForce] Chatter REST Api User Photo Example

I'm looking for an example that uses the Chatter REST Api to upload a User Photo.

The documentation explains the syntax, but they don't give an example of the HTTP form data to POST.

Elsewhere in the documentation, an example is given for posting a file, though:

POST /services/data/v26.0/chatter/feeds/user-profile/005x0000001TBMVAA4/feed-items HTTP/1.1
Authorization: Bearer 00Dx00000001hQY!ARUAQPg.NZBUVAWJh8Dd8UtWHE2k1o0wQuyN9XcRjqZNnuiGJ_YdaQ5uqGa.j7iK4VKF6jzsP3PBvDlQyJ8.MKI983iDUC78
User-Agent: Jakarta Commons-HttpClient/3.0.1
Host: instance_name
Content-Length: 897
Content-Type: multipart/form-data; boundary=0HWq8x4y4DSQ4fqjXt6MinVMyqbf1r
--0HWq8x4y4DSQ4fqjXt6MinVMyqbf1r
Content-Disposition: form-data; name="xml"
Content-Type: application/xml; charset=UTF-8
Content-Transfer-Encoding: 8bit

<feedItem>
   <body>
      <messageSegments>
         <segment>
            <text>High priority content </text>
            <type>Text</type>
         </segment>
         <segment>
             <tag>important</tag>
            <type>Hashtag</type>
         </segment>
         <segment>
            <text>Please review this as soon as possible</text>
            <type>Text</type>
         </segment>
      </messageSegments>
   </body>
   <attachment>
      <desc>Quarterly review</desc>
      <title>2012_q1</title>
   </attachment>
</feedItem>
--0HWq8x4y4DSQ4fqjXt6MinVMyqbf1r
Content-Disposition: form-data; name="feedItemFileUpload"; title="2012_q1_review.ppt"
Content-Type: application/octet-stream; charset=ISO-8859-1
Content-Transfer-Encoding: binary

binary stream of file

--0HWq8x4y4DSQ4fqjXt6MinVMyqbf1r--

How would the example above be modified to upload a User Photo?

Best Answer

Here's the POST data that ended up working for me. I had to use JSON instead of XML in the first part of the POST (after the first boundary) because XML was returning an UNKNOWN EXCEPTION. I'm sure there's a way to use XML, though; I just don't know how.

POST https://cs7.salesforce.com/services/data/v26.0/chatter/users/[my-user-id]/photo HTTP/1.1
Accept: application/xml
Content-Type: multipart/form-data; boundary=8cfa66b1737e032
Authorization: OAuth [my-session-id]
Host: [my-Salesforce-instance-name].salesforce.com
Content-Length: 32075
Expect: 100-continue

--8cfa66b1737e032
Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=UTF-8

{}
--8cfa66b1737e032
Content-Disposition: form-data; name="fileUpload"; filename="photo.jpg"
Content-Type: application/octet-stream; charset=ISO-8859-1
Content-Transfer-Encoding: binary

[binary-file-bytes]
--8cfa66b1737e032--