I have a web view in my app that sends request to server with results of a poll that user answered. I need to edit that request and add a UIImage to it (that is not saved anywhere on my device as a file). I catch the request in webView:shouldStartLoadWithRequest:navigationType:
and then I thought the best thing to do was editing httpbody. To make it a bit easier for me, the guy who is responsible for this app server-side added a hidden button to the poll html, so request's http body has a field for image. The field is of course empty, since user can't even see the button and select an image on his own.
This is the part of request.httpBody (decoded with utf-8).
------WebKitFormBoundaryAe9BONo6gQNiuApf
Content-Disposition: form-data; name="photo"; filename=""
Content-Type: application/octet-stream
------WebKitFormBoundaryAe9BONo6gQNiuApf
How should I add image to this request to make it still understandable for the server?
I have found a solution. If anyone has the same problem again, here it is:
I decode request's httpBody as UTF-8 string, find range of "application/octet-stream", then go 4 characters farther and the result is the index where I'm going to put my image in. I divide the httpBody string into 2 parts: first is everything before image, second is everything after image. I encode first part into NSData, append UIImageJPEGRepresentation
of my UIImage and then I append encoded second part of the string. I set the result as the request's httpBody and start it myself, returning NO in webView:shouldStartLoadWithRequest:navigationType:
.