gpt4 book ai didi

ios - POST请求响应?

转载 作者:行者123 更新时间:2023-12-01 17:49:30 25 4
gpt4 key购买 nike

我正在尝试使用消息正文中的某些参数构造一个POST请求,但正在努力处理/查看响应。

这是我尝试在Obj C中重新创建的请求:

enter image description here

我对创建此请求的代码有些熟悉,但是正在努力将响应解析为有意义的数据。这是我的方法当前的外观(看起来很成功):

  NSLog(@"WEB SERVICE CALLED!");

NSURL *aUrl = [NSURL URLWithString:@"xxx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];

[request setHTTPMethod:@"POST"];
NSString *postString = @"grant_type=password&username=xxx&password=xxx";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
delegate:self];

if(connection) {
NSLog(@"Connection Successful");
} else {
NSLog(@"Connection could not be made");
}
}

我如何看待我的回应?在此先感谢您的帮助!

更新

我添加了这一点,可以看到我的响应给了我代码200(成功),但是数据对象看起来像需要十六进制的字符串一样需要被序列化。这是我正在处理响应/数据/错误的方法:
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

// your data or an error will be ready here
NSLog(@"RESPONSE: %@",response);

NSLog(@"DATA: %@",data);

NSLog(@"ERROR: %@",error);
}];

数据对象的日志如下所示:
enter image description here

最佳答案

NSError *error;
id obj= [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!obj) {
NSLog(@"JSON parse error: %@", error);
} else {
NSLog(@"obj= %@", obj);
}

关于ios - POST请求响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35560975/

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