卷曲动作如:
curl -XPOST http://api.zoomeye.org/user/login -d
'{
"username": "foo@bar.com",
"password": "foobar"
}'
我想在iOS中实现它,所以我尝试
NSDictionary *params = @{
@"username" : username,
@"password" : password
};
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
[mgr POST:@"http://api.zoomeye.org/user/login" parameters:user success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"work"); //test
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error"); //test
}];
但我总是得到错误。
我如何转换它?
答案 0 :(得分:1)
最后我自己解决了这个问题。
NSURL *url = [NSURL URLWithString:@"http://api.zoomeye.org/user/login"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
NSData *postdata = [NSJSONSerialization dataWithJSONObject:params options:NSJSONReadingAllowFragments error:nil];
[request setHTTPBody:postdata];
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
我得到了正确的答复。