xcode AFNetworking 发布表单数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14504766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AFNetworking Post Form-Data
提问by hokiewalrus
I am trying to do a simple POST request with Form-Data to an api but I always get a status 401 in return.
我正在尝试使用 Form-Data 对 api 执行简单的 POST 请求,但我总是得到状态 401 作为回报。
I can easily make the call outside of xcode (in Postman for instance) with success, all I do is POST to the url with key-value pairs of type Form-Data but in xcode it always fails.
我可以轻松地在 xcode 之外(例如在 Postman 中)成功地进行调用,我所做的就是使用 Form-Data 类型的键值对 POST 到 url,但在 xcode 中它总是失败。
Here is my AFHTTPClient:
这是我的 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;
}
I've also attempted creating a request with a requestOperation:
我还尝试使用 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;
}
But I get the exact same error all the time: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 401"
. I'm expecting JSON feedback, you can see the success condition in the attached picture. I suspect it's something very simple but I'm pulling my hair out trying to find it, I'm quite new to networking. Any help would be awesome, thanks.
但我一直得到完全相同的错误:Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 401"
. 我期待 JSON 反馈,您可以在附图中看到成功条件。我怀疑这是非常简单的事情,但我正在努力寻找它,我对网络还很陌生。任何帮助都会很棒,谢谢。
UPDATE
更新
Since the Postman request has blank params and headers, I figured I needed to get to the form body. Changing the request to:
由于 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;
}
Now gives me a 404 error, which is what I would expect without a valid email and password, I just need to figure out how to add one to the body of the form, anything I try to add gives me the "request body stream exhausted" error.
现在给了我一个 404 错误,这是我在没有有效电子邮件和密码的情况下所期望的,我只需要弄清楚如何将一个添加到表单的正文中,我尝试添加的任何内容都会给我“请求正文流耗尽“ 错误。
回答by hokiewalrus
Got it, turns out it's a little verbose to add key/value pairs to form-data but I accomplished it like so:
明白了,事实证明将键/值对添加到表单数据有点冗长,但我是这样完成的:
NSMutableData *email = [[NSMutableData alloc] init];
NSMutableData *password = [[NSMutableData alloc] init];
[email appendData:[[NSString stringWithFormat:@"[email protected]"] dataUsingEncoding:NSUTF8StringEncoding]];
[password appendData:[[NSString stringWithFormat:@"qwerty"] dataUsingEncoding:NSUTF8StringEncoding]];
[formData appendPartWithFormData:email name:@"email"];
[formData appendPartWithFormData:password name:@"password"];