2013-04-01 44 views
1

我試圖從我的iOS應用程序發送一個請求到服務器。Objective C HTTP應用程序和服務器之間的請求參數丟失

curl -d "uid=1&type=robbery&x=41.66505&y=-93.73046" http://thawing-ravine-5632.herokuapp.com/sos 

而這裏所提出請求的Objective-C代碼:使用curl時,要求工作

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
            initWithURL:[NSURL 
               URLWithString:@"http://thawing-ravine-5632.herokuapp.com/sos"]]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:x 
    forHTTPHeaderField:@"x"]; 
    [request setValue:y 
    forHTTPHeaderField:@"y"]; 
    [request setValue:type 
    forHTTPHeaderField:@"type"]; 
    [request setValue:uid 
    forHTTPHeaderField:@"uid"]; 

    NSURLConnection * postOutput =[[NSURLConnection alloc] 
    initWithRequest:request 
    delegate:self]; 

最後,didReceiveData記錄以下:

2013-03-31 20:29:32.248 THST Mobile[14720:11c03] connectionDidReceiveData {"student_found":0,"school_found":0,"officer_found":0,"text_sent":0,"uid":0,"x":0.0,"y":0.0,"type":null} 

但記錄請求之前的變量顯示它們確實在提出請求之前在應用程序中實例化。在接收到請求時,SSH服務器顯示它們都只是零字符串或空字符串。所以我的Objective-C代碼發出post請求有什麼問題 - 可能是我設置參數的方式?

回答

9

您將值設置爲標題,而不是請求的正文。

NSString *params = @"x=1&y=bla&z=foo"; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; 
相關問題