arcade16
arcade16

Reputation: 1535

PUT request status code 500?

I have an implementation of a PUT request in Objective C. The request is successful when executed externally from iOS / Objective C using Postman but returns an error (status code 500) when called within Objective C. As far as I can see, the implementation mirrors the way the call is set up in Postman. Here is the call I am trying to mirror using Objective C:

enter image description here

enter image description here

And here is my implementation in 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);
                               }

                           }];
}

Any input / help would be greatly appreciated, thanks in advance!

Upvotes: 2

Views: 1548

Answers (1)

Lou Franco
Lou Franco

Reputation: 89222

This line:

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

Doesn't match what you are showing in Postman.

  1. Missing { } around the object
  2. Missing " " around the field and value
  3. The Accept and Content-type header are missing

Upvotes: 2

Related Questions