NSURLRequest and HTTP PUT?
NSURLRequest and HTTP PUT?
- Subject: NSURLRequest and HTTP PUT?
- From: Jan van Doorn <email@hidden>
- Date: Mon, 24 May 2004 20:24:06 -0700
Hello,
Sorry if this is easy to be found in docs or on the web, but I tried,
and couldn't find anything remotely helpful. I am still pretty new to
Cocoa, and even newer to this list, so again, please excuse me if I am
asking the wrong question, or the wrong people.
I am trying to do a HTTP PUT from my Cocoa application. I used Neon
(
http://www.webdav.org/neon) before, that worked, but I had some
stability problems, and want a bit more flexibility, so now, I am
looking into NSURL* (Request, Response, Connection) to do the job.
My problem is that it seems to never actually send the body with the
PUT request. Below is a little snippet of code. It sends the request,
but, there is no Content-Length: header, and the body is 0 bytes. If I
just change the "[request setHTTPMethod:@"PUT"];" to "[request
setHTTPMethod:@"POST"];" it actually will include the body in the
request, and gives me a Content-Length: header for free. But I want to
PUT, not POST. I used ethereal to verify the contents of the request
when it arrives at the server.
I am pretty sure the NSURLRequest should support the "PUT" method, it
does support the more exotic (webdav) MKCOL method, I got that working
in just a couple of minutes.... There's no body with that though.
----- code snippet -----
NSString *sourceFileName = "/Users/JvD/tmp/testfile.txt";
NSString *urlString=[[NSString alloc]
initWithString:@"
http://myserver.mydomain.com/testfile.txt"];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:urlString]];
NSData *fileData = [[NSData alloc]
initWithContentsOfFile:sourceFileName];
[request setHTTPMethod:@"PUT"];
[request setHTTPBody:[fileData retain]]; // I tried with and
without retain message
NSData *body = [request HTTPBody];
NSLog(@"data: %@", [[NSString alloc]initWith
Data:body
encoding:NSUTF8StringEncoding]); // this show the contents of the txt
file
NSData *respData;
NSURLResponse *response;
NSError *error;
respData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
----- /code snippet -----
Any and all help would be appreciated.
Cheers,
JvD
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.