ios - 如果通过 iOS 客户端发送请求,则无法识别参数,Curl 相同的 url 按预期工作
<p><p>所以我从 ios 应用程序向我的服务器发出 GET 请求。我得到的响应是一个空数组,但我期待一些数据。我使用 Curl 到相同的 url(从日志中复制粘贴)并且我得到了一些数据,所以可能它是我的应用程序。但是当我更改 url 并尝试记录响应时,我确实得到了一个响应,表明我的应用程序很好。现在我不知道该怎么办。 </p>
<p>我的服务器是一个 Rails 应用程序。代码如下:</p>
<pre><code>def search
@query = params[:user]
q="%#{@query}%"
users = []
@users = User.where("username like ?", q)
for user in @users
if (user.avatar)
users << {username: user.username, avatar: user.avatar.image.thumbnail_dp}
else
users << {username: user.username, avatar: ""}
end
end
respond_to do |format|
format.json {render :json => users.to_json }
end
end
</code></pre>
<p>这是我的 Objective-C 代码</p>
<pre><code>-(void)getFriends:(NSString *)query{
Keychain *keychain = [init];
NSString *authToken = ;
NSString *url = @"http://url.json";
NSString *urlString = ;
NSURL *main_url = ;
NSLog(@"url = %@", main_url);
self.remote_response = [ init];
NSMutableURLRequest *request = [init];
;
;
;
;
;
NSLog(@"request ===%@", request);
self.remote_connection = [ initWithRequest:request delegate:self];
;
.networkActivityIndicatorVisible = YES;
}
</code></pre>
<p>这里是委托(delegate)方法:</p>
<pre><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
;
NSLog(@"DID RECEIVE RESPONSE");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
UIAlertView *alert = [initWithTitle:@"Error"
message:(@"Could not get friends with error %@", error)
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil , nil];
;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
;
NSLog(@"Did receive data");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *error;
NSString *responseText = [ initWithData:self.remote_response
encoding:NSUTF8StringEncoding];
NSData *data = ;
NSLog(@"Response ==> %@", responseText);
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers error:&error];
if(>0){
NSLog(@"Response::::%@", responseDictionary);
}
.networkActivityIndicatorVisible = NO;
}
</code></pre>
<p>这是我得到的 NSURLResponse 日志</p>
<pre><code>DID RECEIVE RESPONSE
Did receive data
Response ==> []
</code></pre>
<p>编辑:::
发现了一些东西。没有设置参数我不知道为什么当我 NSLog 时,我看到了参数。</p>
<p>如果我 curl ,这里是日志</p>
<pre><code>2013-09-05T07:30:58.222613+00:00 app:Started GET "/search.json?user=heeman" for 58.137.173.76 at 2013-09-05 07:30:58 +0000
2013-09-05T07:30:58.428178+00:00 app: Processing by UsersController#search as JSON
2013-09-05T07:30:58.428178+00:00 app: Parameters: {"user"=>"heeman"}
2013-09-05T07:30:58.428178+00:00 app: Completed 200 OK in 200ms (Views: 0.1ms | ActiveRecord: 7.5ms)
</code></pre>
<p>这是通过我的 ios 客户端的日志</p>
<pre><code>2013-09-05T07:35:35.766319+00:00 app: Started GET "/search.json?user=heeman" for 58.137.173.76 at 2013-09-05 07:35:35 +0000
2013-09-05T07:35:35.778962+00:00 heroku: at=info method=GET path=/search.json?user=heeman host="host" fwd="58.137.173.76" dyno=web.1 connect=1ms service=51ms status=304 bytes=0
2013-09-05T07:35:35.773667+00:00 app: Processing by UsersController#search as JSON
2013-09-05T07:35:35.773667+00:00 app: Parameters: {"user"=>{}}
2013-09-05T07:35:35.773667+00:00 app: Completed 200 OK in 3ms (Views: 0.2ms | ActiveRecord: 1.2ms)
</code></pre>
<p>如果您在日志中看到,url 确实显示参数 <code>method=GET path=/search.json?user=heeman</code> 但随后 <code>Parameters: {"user"=>{ }}</code> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>删除请求中的 <code>Content-type</code> 设置。您不包括任何数据主体,只包括参数,因为这是 GET,而不是 PUT 或 POST。 GET 请求不应包含 <code>Content-type</code>(它们应仅包含 <code>Accept</code>),尽管这不是硬性要求,其影响取决于所使用的服务器处理请求。</p></p>
<p style="font-size: 20px;">关于ios - 如果通过 iOS 客户端发送请求,则无法识别参数,Curl 相同的 url 按预期工作,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18629605/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18629605/
</a>
</p>
页:
[1]