I'm trying to upload an image sent from my iOS app to my bucket storage for app engine and I'm using php. Now let me be clear the example google gave with the web form works perfectly with my handler and it is commented out below So I'm sure my handler is fine and dandy also it works directly with my handler before using app engine and the storage bucket. For anyone unfamiliar with createUploadUrl Here is the documentation on it.
<?php
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
$options = [ 'gs_bucket_name' => 'my-bucket-name' ];
$upload_url = CloudStorageTools::createUploadUrl('/CustomPostHandler.php', $options);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: multipart'
)
);
$context = stream_context_create($opts);
$result = file_get_contents($upload_url,false,$context);
echo $result;
/*<form action="<?php echo $upload_url?>" enctype="multipart/form-data" method="post">
Files to upload: <br>
<input type="file" name="userfile" size="10000000">
<input type="submit" value="Send">
</form>*/
?>
Because i want to use storage bucket i call the createUploadUrl because thats what google wants. I'm using swift on the iOS front end and the error code I'm getting for echoing the $result is "The request's content type is not accepted on this url" I have tried to use 'multipart/form-data' for the php part and it gave me the error 'Bad type use multipart' or something along those lines.
the iOS code is posted below and again ill say this works perfectly when not running on app engine but on a local server. The error message is my obvious problem but i think specifically it may have something to do with the octet-stream I'm not sure. Any help would be appreciated!!! Thank you!
var postString = "\(PHP_PATH)UploadTest.php"
var request = NSMutableURLRequest(URL: NSURL(string: postString)!)
request.HTTPMethod = "POST"
var boundary = "---------------------------14737809831466499882746641449"
var contentType = "multipart/form-data; boundary=\(boundary)"
request.addValue(contentType, forHTTPHeaderField: "Content-Type")
println(request)
var body = NSMutableData()
body.appendData(("
--\(boundary)
" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(("Content-Disposition: form-data; name=\"userfile\"; filename=\"\(fileName)\"
" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(("Content-Type: application/octet-stream
" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(imageData)
body.appendData(("
--\(boundary)--
" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
request.HTTPBody = body
var returnData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
println(NSString(data: returnData!, encoding: NSUTF8StringEncoding)!)
</div>