405 Method Not Allowed

坚强是说给别人听的谎言 提交于 2019-12-11 03:03:10

问题


I've trying to access https://api.box.com/2.0/files but i receive Expected status code in (200-299), got 405 in my response (from AFNetworking).

Before sending the request i've get my auth_token from the server.

Code

- (void)getFileListing:(NSString*)apiKey
{
    if(apiKey == nil) { apiKey = kBoxNetApiKey; }

    NSDictionary *boxAuth = [[NSUserDefaults standardUserDefaults] objectForKey:kBoxNetUserDefaultsKey];

    if([boxAuth objectForKey:@"auth_token"] != nil) {
        NSURL *url = [NSURL URLWithString:@"https://api.box.com/2.0/files"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"GET"];

        DLog(@"auth_token: %@", [boxAuth objectForKey:@"auth_token"]);
        DLog(@"apiKey: %@", apiKey);

        NSString *auth = [NSString stringWithFormat:@"BoxAuth api_key=%@&auth_token=%@", apiKey, [boxAuth objectForKey:@"auth_token"]];
        [request setValue:auth forHTTPHeaderField:@"Authorization"];

        AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

            DLog(@"JSON: %@", JSON);

        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

            DLog(@"error: %@", error);
            DLog(@"JSON: %@", JSON);

        }];

        [operation start];
    }
}

*Error

__29-[BoxNetAuth getFileListing:]_block_invoke_081 [Line 75] error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 405" UserInfo=0xa0b8740 {NSLocalizedRecoverySuggestion={"type":"error","status":405,"code":"method_not_allowed","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Method Not Allowed","request_id":"183259878350bcd62a62f1b"}, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest https://api.box.com/2.0/files>, NSErrorFailingURLKey=https://api.box.com/2.0/files, NSLocalizedDescription=Expected status code in (200-299), got 405, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xa6c9840>}


回答1:


You can't do a GET on https://api.box.com/2.0/files as it needs a resource ID like: GET https://api.box.com/2.0/files/12345

You can POST to https://api.box.com/2.0/files/content to upload a new file or you can GET https://api.box.com/2.0/folders/0 to get the root folder



来源:https://stackoverflow.com/questions/13687840/405-method-not-allowed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!