gpt4 book ai didi

ios - PUT请求状态码500?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:29 28 4
gpt4 key购买 nike

我在 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)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);
}

}];
}

任何输入/帮助将不胜感激,提前致谢!

最佳答案

这一行:

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

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

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

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com