I'm having some issue with uploading file attachments for News in Valence API. I'm using PHP and when I try to make a post request, the action never gets completed. I'm running it on command line so I have to kill the process to terminate the program.
My file data and details(Name, Type and Size) are stored in the database and I'm using just one call to post everything i.e. the News in JSON Format and the file attachments. I'm using the following format for the post message (As mentioned in the API docs):
Content-Type: multipart/mixed; boundary=e9e1c17c958e2a89a192bf5d3f7d1ffd Content-Length: 46538
--e9e1c17c958e2a89a192bf5d3f7d1ffd Content-Type: application/json
{"Title":"Some News","Body":{"Text":"This </span>is a very </span>colorful title</span>","Html":"This </span>is a very </span>colorful title</span>"},"StartDate":"2013-11-06T05:00:00.000Z","EndDate":"2013-11-30T05:00:00.000Z","IsGlobal":false,"IsPublished":true,"ShowOnlyInCourseOfferings":false}
--e9e1c17c958e2a89a192bf5d3f7d1ffd Content-Disposition: form-data; name="FC.png"; filename="FC.png"
Content-Type: image/png
{} --e9e1c17c958e2a89a192bf5d3f7d1ffd--
I can't see the file content due to encoding I guess. I'm calculating the content-length in this message as: string length of the JSON data + filesize in bytes.
My very speicific question is that can one post files from the database directly or it's recommended to write the file to the disk first then try posting. I would also appreciate if someone can point me to an example online where there's a pre-existing sample code using PHP for this.
One more thing. If I do not include the file-size in bytes for the content-length, then it works fine except for the file attachment. It is 29 bytes in size and when I click on it, it opens with corrupt data error.
Any kind of help will be appreciated. Thanks.
Found what I was doing wrong. Needed to UTF encode the file document data before using json_encode on it. The data was getting lost during json_encode as it does not understands special characters. Code snippet: