OStack程序员社区-中国程序员成长平台

标题: ios - PUT 请求状态码 500? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:41
标题: ios - PUT 请求状态码 500?

我在 Objective C 中实现了一个 PUT 请求。使用 Postman 从 iOS/Objective C 外部执行该请求时成功,但在 Objective C 中调用时返回错误(状态代码 500)。据我所知,该实现反射(reflect)了在 Postman 中设置调用的方式。这是我尝试使用 Objective C 镜像的调用:

enter image description here

enter image description here

这是我在 Objective C 中的实现:

- (void)callUnregisterForPushNotificationsNSString *)accessToken
                               pushTokenNSString *)pushToken
                         completionBlockvoid (^)(NSMutableArray *resultsArray))completion{

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

    NSLog(@"USH 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"UT"];

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

    [request addValue:appendToken forHTTPHeaderField"Authorization"];

    NSString *postString = [NSString stringWithFormat"Guid=%@",pushToken];
    NSLog(@"OST 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);
                               }

                           }];
}

任何输入/帮助将不胜感激,在此先感谢!



Best Answer-推荐答案


这一行:

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

与您在 Postman 中显示的内容不符。

  1. 对象周围缺少 { }
  2. 字段和值周围缺少 ""
  3. 缺少 AcceptContent-type header

关于ios - PUT 请求状态码 500?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35709205/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4