objective c - How can I debug an HTTP POST sent from my iPhone app? -
i'm using next code upload image server:
// dictionary holds post parameters. can set post parameters server accepts or programmed accept. nsmutabledictionary* _params = [[nsmutabledictionary alloc] init]; datamanager *manager = [datamanager sharedmanager]; [_params setobject:[manager token] forkey:@"token"]; [_params setobject:[nsstring stringwithformat:@"%d",[manager site_id]] forkey:@"site"]; nsstring *boundaryconstant = @"----------v2ymhfg03ehbqgzcako6jy"; nsstring* fileparamconstant = @"image"; nsurl* requesturl = [nsurl urlwithstring:@"https://mysite.com/api/upload/"]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; [request setcachepolicy:nsurlrequestreloadignoringlocalcachedata]; [request sethttpshouldhandlecookies:no]; [request settimeoutinterval:30]; [request sethttpmethod:@"post"]; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@", boundaryconstant]; [request setvalue:contenttype forhttpheaderfield: @"content-type"]; nsmutabledata *body = [nsmutabledata data]; (nsstring *param in _params) { [body appenddata:[[nsstring stringwithformat:@"--%@\r\n", boundaryconstant] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"%@\"\r\n\r\n", param] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"%@\r\n", [_params objectforkey:param]] datausingencoding:nsutf8stringencoding]]; } nsdata *imagedata = uiimagejpegrepresentation(saveimage, 1.0); if (imagedata) { [body appenddata:[[nsstring stringwithformat:@"--%@\r\n", boundaryconstant] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", fileparamconstant] datausingencoding:nsutf8stringencoding]]; [body appenddata:[@"content-type: image/jpeg\r\n\r\n" datausingencoding:nsutf8stringencoding]]; [body appenddata:imagedata]; [body appenddata:[[nsstring stringwithformat:@"\r\n"] datausingencoding:nsutf8stringencoding]]; } [body appenddata:[[nsstring stringwithformat:@"--%@--\r\n", boundaryconstant] datausingencoding:nsutf8stringencoding]]; [request sethttpbody:body]; nslog(@"%@",body); nsstring *postlength = [nsstring stringwithformat:@"%d", [body length]]; nslog(@"sending request length: %@",postlength); [request setvalue:postlength forhttpheaderfield:@"content-length"]; [request seturl:requesturl]; nsurlconnection *conn = [[nsurlconnection alloc]initwithrequest:request delegate:self];
watching server logs seek , upload, don't lot of information:
82.132.x.x - - [06/feb/2013:15:37:45 +0000] "-" 400 0 "-" "-"
compare query same app:
82.132.x.x - - [06/feb/2013:15:36:13 +0000] "get /api/sites/?token=blah http/1.1" 200 269 "-" "my%20app/1.0 cfnetwork/609.1.4 darwin/13.0.0"
how can figure out going wrong? request malformed in way, causing error 400. there tool can utilize inspect it?
wireshark should allow inspect what's beingness sent / received.http://www.wireshark.org/download.html
objective-c http
No comments:
Post a Comment