2016-02-29 132 views
2

我在Objective C中實現了PUT請求。當使用Postman從iOS/Objective C執行外部請求時,請求成功,但在Objective C中調用時返回錯誤(狀態碼500)。據我所知,該實現反映了Postman中調用的方式。這裏是我嘗試使用目標C鏡像呼叫:PUT請求狀態碼500?

enter image description here

enter image description here

,這裏是我的目標C實現:

- (void)callUnregisterForPushNotifications:(NSString *)accessToken 
           pushToken:(NSString *)pushToken 
         completionBlock:(void (^)(NSMutableArray *resultsArray))completion{ 

    NSLog(@"UNREGISTER FOR PUSH CALLED!"); 

    NSLog(@"PUSH TOKEN %@", pushToken); 

    NSString *appendUrl = @"alerts/unregisterpush/"; 

    NSLog(@"APPEND URL %@",appendUrl); 

    NSURL *unregisterUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", BaseURLString, appendUrl]]; 
    NSLog(@"UNREGISTER URL: %@",unregisterUrl); 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:unregisterUrl 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:30.0]; 

    [request setHTTPMethod:@"PUT"]; 

    NSString *appendToken = [NSString stringWithFormat:@"Bearer %@", accessToken]; 
    NSLog(@"TOKEN: %@",appendToken); 

    [request addValue:appendToken forHTTPHeaderField:@"Authorization"]; 

    NSString *postString = [NSString stringWithFormat:@"Guid=%@",pushToken]; 
    NSLog(@"POST STRING: %@",postString); 
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSLog(@"REQUEST %@",request); 

    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

                   NSLog(@"UNREGISTER PUSH NOTIFICATIONS RESPONSE: %@", response); 
                   NSLog(@"UNREGISTER PUSH NOTIFICATIONS ERROR: %@", error); 
                   NSLog(@"UNREGISTER PUSH NOTIFICATIONS DATA: %@", data); 

           NSData *_data = data;// ... whatever 
           NSMutableString *_string = [NSMutableString stringWithString:@""]; 
           for (int i = 0; i < _data.length; i++) { 
            unsigned char _byte; 
            [_data getBytes:&_byte range:NSMakeRange(i, 1)]; 
            if (_byte >= 32 && _byte < 127) { 
             [_string appendFormat:@"%c", _byte]; 
            } else { 
             [_string appendFormat:@"[%d]", _byte]; 
            } 
           } 
           NSLog(@"UNREGISTER PUSH RESPONSE: %@", _string); 

           id obj= [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 
           if (!obj) { 
            //NSLog(@"REGISTER PUSH NOTIFICATIONS ERROR: %@", error); 

           } else { 
            //NSLog(@"REGISTER PUSH NOTIFICATIONS DATA: %@", obj); 

            if(completion) { 
             [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
             completion((NSMutableArray*)obj); 
            } 

            //self.accessToken = [obj valueForKey:@"access_token"]; 
            //NSLog(@"ACCESS TOKEN: %@",self.accessToken); 
           } 

          }]; 
} 

任何輸入/幫助將不勝感謝,提前致謝!

+1

500是 「內部服務器錯誤」。看看服務器日誌文件,你會知道什麼是錯誤的 – Jens

+0

我得到了以下錯誤:「處理HTTP請求導致異常。請參閱此異常的'響應'屬性返回的HTTP響應的詳細信息。 「 – arcade16

+0

然後在查看我回復的響應後,它會顯示「{」Code「:null,」Message「:」請聯繫服務器管理員!「}」 – arcade16

回答

2

這條線:

NSString *postString = [NSString stringWithFormat:@"Guid=%@",pushToken]; 

不符合您的顯示郵差什麼。

  1. 缺少{ }圍繞對象
  2. 缺少圍繞字段和值
  3. AcceptContent-type" "丟失
+0

你部分正確,我還需要將等號「=」改成冒號「:」謝謝! – arcade16