为什么 RestKit 返回与 NSURLConnection 不同的响应

发布于 2024-12-27 18:17:28 字数 1713 浏览 0 评论 0原文

我在项目中使用 RestKit 并收到意外响应“---”,因此我尝试使用标准 NSURLConnection,但得到了正确的响应 - json 格式的字符串。任何人都可以弄清楚这两个请求之间有什么区别吗?如果它有所不同,则响应的内容类型为“text/yaml”。

这是返回 '---' 的 RKRequest 版本

NSURL *URL = [NSURL URLWithString:URL_STRING];
RKRequest *request = [RKRequest requestWithURL:URL delegate:self];
request.queue = [RKClient sharedClient].requestQueue;
request.authenticationType = RKRequestAuthenticationTypeHTTP;
request.method = RKRequestMethodPOST;
request.params = requestParams;
[request send]; 

这是返回 json/yaml 格式的适当格式响应的 NSURLConnection 版本。

NSURL *url = [NSURL URLWithString:URL_STRING];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                               timeoutInterval:60.0];

NSData *requestData = [NSData dataWithBytes:[requestParams UTF8String] 
                                     length:[requestParams length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

更新:

好吧,现在事情变得非常奇怪 - NSURLConnection 和 RestKit 都完全停止工作了。如果有人可以提供帮助,我们将不胜感激,这是我使用的 cURL 抛出终端来验证结果(这仍然有效)。

curl -i -H "Content-Type: application/json" -H "Accept: application/json" -d '{"username":"username","password":"password"}' http://example.com/login

我的 requestParams 是 @"{\"用户名\":\"用户名\",\"密码\":\"密码\"}"

I am using RestKit in a project and was getting an unexpected response '---', so I tried to use a standard NSURLConnection but I got the correct response - a json formatted string. Can any one figure out what is the difference between these two requests? If it makes a different the content-type is "text/yaml" for the response.

Here is the RKRequest version which returns '---'

NSURL *URL = [NSURL URLWithString:URL_STRING];
RKRequest *request = [RKRequest requestWithURL:URL delegate:self];
request.queue = [RKClient sharedClient].requestQueue;
request.authenticationType = RKRequestAuthenticationTypeHTTP;
request.method = RKRequestMethodPOST;
request.params = requestParams;
[request send]; 

Here is the NSURLConnection version which returns an appropriately formatted response in json/yaml.

NSURL *url = [NSURL URLWithString:URL_STRING];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                               timeoutInterval:60.0];

NSData *requestData = [NSData dataWithBytes:[requestParams UTF8String] 
                                     length:[requestParams length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

UPDATE:

Okay, now things are getting really weird - it stopped working completely for both the NSURLConnection and RestKit. If anyone can help it would be appreciated, here is the cURL that I use threw the terminal to verify results (this still works).

curl -i -H "Content-Type: application/json" -H "Accept: application/json" -d '{"username":"username","password":"password"}' http://example.com/login

And my requestParams is @"{\"username\":\"username\",\"password\":\"password\"}"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文