7

Form-Data を使用して API に対して単純な POST リクエストを実行しようとしていますが、常にステータス 401 が返されます。

Xcode の外部 (たとえば Postman) で簡単に呼び出しを成功させることができます。Form-Data 型のキーと値のペアを使用して URL に POST するだけですが、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 に答える