如何在 objective-c 中将参数作为键传递,将其他参数作为正文传递
how to pass parameter as key and other parameters as body in objective-c
我需要在 body 中传递 act、email、userid,在 key 中传递 session_id。我正在使用 Postman,我得到了想要的结果。但是我无法在 Objective C 中完成。我正在使用 AFNetworking
这就是我想要达到的目标。
-(void)CallUpdateAPI
{
AFHTTPSessionManager *apicall = [AFHTTPSessionManager manager];
apicall.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableDictionary *parameters2 =[[NSMutableDictionary alloc]init];
[parameters2 setObject:[[[NSUserDefaults standardUserDefaults] objectForKey:@"userlist"] objectForKey :@"userid"] forKey:@"userid"];
NSNumber *v1 = [NSNumber numberWithInt:1];
[parameters2 setValue:v1 forKey:@"act"];
[parameters2 setObject:[[[NSUserDefaults standardUserDefaults] objectForKey:@"userlist"] objectForKey :@"session_id"] forKey:@"session_id"];
[parameters2 setObject:_email.text forKey:@"email"];
NSLog(@"Server response : %@ ",parameters2);
[apicall POST:@"https://portal.blockage.io/api/v1/user/kyc_update" parameters:parameters2 progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
NSError *e = nil;
NSMutableDictionary *resultFromPost2 = [NSJSONSerialization JSONObjectWithData: responseObject options: 0 error: &e];
//NSLog(@"Server response : %@ %@",resultFromPost2,e);
//...
}
//...
}
使用合适的正文参数构建字典:
NSDictionary *bodyPArams = @{@"xxx":@"xxx",
@"xxx":@"xxx",
@"xxx":@"xxx",
@"xxx":@"xxx"};
并调用POST方法:
[apicall POST:@"https://portal.blockage.io/api/v1/user/kyc_update" parameters:bodyPArams progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
您的 session_id 参数是 "Get" 参数。您需要将其设置在 url 而不是正文中。
"https://portal.blockage.io/api/v1/user/kyc_update?session_id=YourSessionID"
你可以使用这个:
NSString * urlString = [NSString stringWithFormat:@"https://portal.blockage.io/api/v1/user/kyc_update?session_id=%@", [[[NSUserDefaults standardUserDefaults] objectForKey:@"userlist"] objectForKey :@"session_id"];
[apicall POST:urlString parameters:parameters2 progress:nil success:^(NSURLSessionTask *task, id responseObject)
我需要在 body 中传递 act、email、userid,在 key 中传递 session_id。我正在使用 Postman,我得到了想要的结果。但是我无法在 Objective C 中完成。我正在使用 AFNetworking
这就是我想要达到的目标。
-(void)CallUpdateAPI
{
AFHTTPSessionManager *apicall = [AFHTTPSessionManager manager];
apicall.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableDictionary *parameters2 =[[NSMutableDictionary alloc]init];
[parameters2 setObject:[[[NSUserDefaults standardUserDefaults] objectForKey:@"userlist"] objectForKey :@"userid"] forKey:@"userid"];
NSNumber *v1 = [NSNumber numberWithInt:1];
[parameters2 setValue:v1 forKey:@"act"];
[parameters2 setObject:[[[NSUserDefaults standardUserDefaults] objectForKey:@"userlist"] objectForKey :@"session_id"] forKey:@"session_id"];
[parameters2 setObject:_email.text forKey:@"email"];
NSLog(@"Server response : %@ ",parameters2);
[apicall POST:@"https://portal.blockage.io/api/v1/user/kyc_update" parameters:parameters2 progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
NSError *e = nil;
NSMutableDictionary *resultFromPost2 = [NSJSONSerialization JSONObjectWithData: responseObject options: 0 error: &e];
//NSLog(@"Server response : %@ %@",resultFromPost2,e);
//...
}
//...
}
使用合适的正文参数构建字典:
NSDictionary *bodyPArams = @{@"xxx":@"xxx",
@"xxx":@"xxx",
@"xxx":@"xxx",
@"xxx":@"xxx"};
并调用POST方法:
[apicall POST:@"https://portal.blockage.io/api/v1/user/kyc_update" parameters:bodyPArams progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
您的 session_id 参数是 "Get" 参数。您需要将其设置在 url 而不是正文中。
"https://portal.blockage.io/api/v1/user/kyc_update?session_id=YourSessionID"
你可以使用这个:
NSString * urlString = [NSString stringWithFormat:@"https://portal.blockage.io/api/v1/user/kyc_update?session_id=%@", [[[NSUserDefaults standardUserDefaults] objectForKey:@"userlist"] objectForKey :@"session_id"];
[apicall POST:urlString parameters:parameters2 progress:nil success:^(NSURLSessionTask *task, id responseObject)