PUT请求状态码500?
PUT request status code 500?
我在 Objective C 中实现了 PUT 请求。使用 Postman 从 iOS / Objective C 外部执行时请求成功,但在 Objective C 内调用时 returns 出现错误(状态代码 500)。据我所知,该实现反映了在 Postman 中设置调用的方式。这是我尝试使用 Objective C:
镜像的调用
这是我在 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 中显示的内容不匹配。
- object
周围缺少 { }
- 字段和值周围缺少
" "
- 缺少
Accept
和 Content-type
header
我在 Objective C 中实现了 PUT 请求。使用 Postman 从 iOS / Objective C 外部执行时请求成功,但在 Objective C 内调用时 returns 出现错误(状态代码 500)。据我所知,该实现反映了在 Postman 中设置调用的方式。这是我尝试使用 Objective C:
镜像的调用这是我在 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 中显示的内容不匹配。
- object 周围缺少
- 字段和值周围缺少
" "
- 缺少
Accept
和Content-type
header
{ }