为什么 RestKit 返回与 NSURLConnection 不同的响应
我在项目中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论