gpt4 book ai didi

ios - 在 POST 请求中发送 JSON 对象 AFNetworking , iOS

转载 作者:行者123 更新时间:2023-11-28 21:23:45 25 4
gpt4 key购买 nike

我正在使用 AFNetworking 将数据发布到网络中。我拥有的信息是 URI,即 baseURL/post_user_info,他们希望 input 作为 包含每个名称的 JSON 对象-值对。 在下面编写的代码中,我在字典中设置了名称-值对。我的问题是如何使其成为 json 并将其作为输入值发送?

NSString *string = [NSString stringWithFormat:@"%@?post_user_info",BaseURLString];
NSString *escapedPath = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

NSDictionary *params = @{@"input_1": @"hello world",
@"input_2": @"my@you.com",
@"input_3": @"newworldorder"};
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:params options:kNilOptions error:nil];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];

[manager POST:escapedPath parameters:[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
NSLog(@"%@",responseObject);
}failure:^(NSURLSessionTask *operation, NSError *error)
{
NSLog(@"%@",[error localizedDescription]);
}];

我已经更新了代码,因为现在我已经生成了 JSON,但是如果我现在运行代码,它会给我错误:400,这意味着如果 input_values 为空,将输出一个错误的请求错误。

最佳答案

@interface AFJSONRequestSerializer : AFHTTPRequestSerializer

AFJSONRequestSerializeris a subclass ofAFHTTPRequestSerializerthat encodes parameters as JSON using NSJSONSerialization.

添加此 AFJSONRequestSerializer 用于发送 JSON 而不是原始数据 添加此并尝试一次

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];

例如

NSString *string = [NSString stringWithFormat:@"%@?post_user_info",BaseURLString];
NSString *escapedPath = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

/************** karthik Code added ******************/
AFHTTPSessionManager* manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
/************** karthik Code added ******************/

NSDictionary *params = @{@"1": @"hello world",
@"2": @"my@you.com",
@"3": @"newworldorder"};
[manager POST:escapedPath parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
NSLog(@"%@",responseObject);
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
NSLog(@"json == %@", json);

} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"%@", [error localizedDescription]);
}];

更新参数

NSDictionary *params = @{@"1": @"hello world",
@"2": @"my@you.com",
@"3": @"newworldorder"};
NSMutableDictionary *modify = [NSMutableDictionary new];
[modify setObject:params forKey:@"input_values"];

你得到输出

enter image description here

关于ios - 在 POST 请求中发送 JSON 对象 AFNetworking , iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38892054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com