gpt4 book ai didi

ios - NSURLRequest 没有将值发布到 Codeigniter API

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:21:35 24 4
gpt4 key购买 nike

你好,

我正在尝试使用基于 Codeigniter 的 API 连接 iOS 并使用 NSURLRequest。API 处于 Debug模式,目前它返回与 json 相同的键值对,与您发布的键值对相同​​。我已经尝试通过 postman 将值发布到链接并且它工作正常,但是当我通过我的 iOS 应用程序发布它时,收到了 json 响应但是应该包含发布值的数组是空的。

这是 iOS 代码片段:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEURL,service]];

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

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

NSString * params = @"authkey=waris";
NSData * postData = [params dataUsingEncoding:NSUTF8StringEncoding];

NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];;
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

NSLog(@"Posting : '%@' to %@",params,url);

[connection start];

这是我通过 postman (A RESTFUL Client for Chrome)发布相同参数时的响应

{
"status": "1",
"data": {
"authkey": "warisali"
}
}

然而,当我从上面的 iOS 代码中查询相同的 API 时,我得到了这个:

{
data = 0;
status = 1;
}

任何有关此事的帮助将不胜感激!

最佳答案

我有同样的问题(不是 CodeIgniter,而是 Ruby ...)尝试这样的事情,解决了我的问题。

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEURL,service]];

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

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

NSDictionary *paramDict = @{@"authkey": @"waris"};
NSError *error = nil;
NSData *postData = [NSJSONSerialization dataWithJSONObject:paramDict options:NSJSONWritingPrettyPrinted error:&error];

if (error)
{
NSLog(@"error while creating data %@", error);
return;
}

NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];;
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

NSLog(@"Posting : '%@' to %@",params,url);

[connection start];

关于ios - NSURLRequest 没有将值发布到 Codeigniter API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25261332/

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