7

我正在尝试使用 Form-Data 对 api 进行简单的 POST 请求,但我总是得到状态 401 作为回报。

我可以轻松地在 xcode 之外(例如在 Postman 中)成功地进行调用,我所做的只是使用 Form-Data 类型的键值对 POST 到 url,但在 xcode 中它总是失败。

这是我的 AFHTTPClient:

@implementation UnpaktAPIClient

+ (id)sharedInstance {
    static UnpaktAPIClient *__sharedInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        __sharedInstance = [[UnpaktAPIClient alloc] initWithBaseURL:[NSURL URLWithString:UnpaktAPIBaseURLString]];
    });
    return __sharedInstance;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (self) {
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
        [self setParameterEncoding:AFFormURLParameterEncoding];
    }
    return self;
}

- (NSDictionary *)login:(NSDictionary *)credentials {

    [[UnpaktAPIClient sharedInstance] postPath:@"/api/v1/users/login"
                                    parameters:credentials
                                        success:^(AFHTTPRequestOperation *operation, id response) {
                                            NSLog(@"success");
                                        }
                                        failure:^(AFHTTPRequestOperation *operation, NSError *error){
                                            NSLog(@"%@", error);
                                        }];

    return nil;
}

我还尝试使用 requestOperation 创建请求:

- (NSDictionary *)login:(NSDictionary *)credentials {

    NSMutableURLRequest *request = [[UnpaktAPIClient sharedInstance] requestWithMethod:@"POST" path:@"/api/v1/users/login" parameters:credentials];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
                                                                                            NSLog(@"success");
                                                                                        }
                                                                                        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id json){
                                                                                            NSLog(@"%@", error);
                                                                                        }];
    [operation start];

    return nil;
}

但我总是得到完全相同的错误:Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 401". 我期待 JSON 反馈,您可以在附图中看到成功条件。我怀疑这是非常简单的事情,但我正在努力寻找它,我对网络很陌生。任何帮助都会很棒,谢谢。邮差成功

更新

由于 Postman 请求有空白参数和标题,我想我需要进入表单正文。将请求更改为:

- (NSDictionary *)login:(NSDictionary *)credentials {

    NSURLRequest *request = [self multipartFormRequestWithMethod:@"POST"
                                                            path:@"/api/v1/users/login"
                                                      parameters:nil
                                       constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

                                       }];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
                                                                                            NSLog(@"success");
                                                                                        }
                                                                                        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id json) {
                                                                                            NSLog(@"%@", error);
                                                                                        }];
    [operation start];
    return nil;
}

现在给我一个 404 错误,这是我在没有有效电子邮件和密码的情况下所期望的,我只需要弄清楚如何将一个添加到表单的正文中,我尝试添加的任何内容都会给我“请求正文流已用尽“ 错误。

4

1 回答 1

9

明白了,事实证明将键/值对添加到表单数据有点冗长,但我是这样完成的:

NSMutableData *email = [[NSMutableData alloc] init];
NSMutableData *password = [[NSMutableData alloc] init];
[email appendData:[[NSString stringWithFormat:@"paul+100@test.com"] dataUsingEncoding:NSUTF8StringEncoding]];
[password appendData:[[NSString stringWithFormat:@"qwerty"] dataUsingEncoding:NSUTF8StringEncoding]];
[formData appendPartWithFormData:email name:@"email"];
[formData appendPartWithFormData:password name:@"password"];
于 2013-01-24T19:44:23.443 回答