I suppose it is pretty simple but I couldn't figure out how to do it. I want to upload a file to using a PUT request to a webservice using AFNetworking library. This is the curl command I used to test the service
mac:~ user$ curl --verbose -T image.jpeg http://server.org:8001/social/test.jpg
* About to connect() to server.org port 8001 (#0)
* Trying 123.45.123.123...
* connected
* Connected to server.org (123.45.123.123) port 8001 (#0)
> PUT /social/test.jpg HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: server.org:8001
> Accept: */*
> Content-Length: 78341
> Expect: 100-continue
>
< HTTP/1.1 100 CONTINUE
< Server: cx1193719-b
< Content-Type: Text/Html
< Accept-Ranges: bytes
< Content-Length: 0
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Server: cx1193719-b
< Content-Type: Text/Html
< Accept-Ranges: bytes
< Content-Length: 0
<
* Connection #0 to host server.org left intact
* Closing connection #0
I've been able to upload file using POST and form data to other web services, I have used PUT requests (with AFHTTPClient and putPath) but still I don't understand how to do that simple file upload.
Thanks for your help!
djibouti33's answer is pretty good in my opinion. One thing to note, however is that the multipartFormRequest method in the answer has since been deprecated. AFNetworking now recommends that you use:
A note here is that you'll want to pass an error in by reference by doing something like the following:
This is part of the AFNetworking FAQ. Check the https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ.
Also if you are using a subclass of
AFHTTPClient
you might want to extend it with something similar to:as suggested in this answer: https://stackoverflow.com/a/9014768/634940.
With that implemented and your
AFHTTPClient
subclass' base URL set tohttp://server.org
you can go ahead and call:I'm using AFNetworking 2.0 and was wondering the same thing. Here's what I ended up with: