gpt4 book ai didi

ios - 如果通过 iOS 客户端发送请求,则无法识别参数,Curl 相同的 url 按预期工作

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

所以我从 iOS 应用向我的服务器发出 GET 请求。我得到的响应是一个空数组,但我希望得到一些数据。我将 Curl 用于相同的 url(从日志中复制粘贴),我得到了一些数据,所以可能是我的应用程序。但是当我更改 url 并尝试记录响应时,我确实收到了一个响应,表明我的应用程序正常。现在我不知道该怎么办。

我的服务器是一个 Rails 应用程序。这是代码:

  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

这是我的 objective-c 代码

-(void)getFriends:(NSString *)query{

Keychain *keychain = [[Keychain alloc]init];
NSString *authToken = [keychain getAuthToken];
NSString *url = @"http://url.json";
NSString *urlString = [NSString stringWithFormat:@"%@?user=%@", url, query ];
NSURL *main_url = [NSURL URLWithString:urlString];
NSLog(@"url = %@", main_url);
self.remote_response = [[NSMutableData alloc] init];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL: main_url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:authToken forHTTPHeaderField:@"X-AUTH-TOKEN"];
NSLog(@"request ===%@", request);
self.remote_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.remote_connection start];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

委托(delegate)方法如下:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[self.remote_response setLength:0];
NSLog(@"DID RECEIVE RESPONSE");
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error"
message:(@"Could not get friends with error %@", error)
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil , nil];
[alert show];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
[self.remote_response appendData:d];
NSLog(@"Did receive data");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *error;
NSString *responseText = [[NSString alloc] initWithData:self.remote_response
encoding:NSUTF8StringEncoding];
NSData *data = [responseText dataUsingEncoding:NSASCIIStringEncoding];
NSLog(@"Response ==> %@", responseText);
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers error:&error];
if([responseDictionary count]>0){
NSLog(@"Response::::%@", responseDictionary);

}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

这是我得到的 NSURLResponse 日志

DID RECEIVE RESPONSE
Did receive data
Response ==> []

编辑:::发现了什么。没有设置参数我不知道为什么当我 NSLog 时,我看到了参数。

如果我 curl 它,这是日志

2013-09-05T07:30:58.222613+00:00 app[web.1]: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[web.1]: Processing by UsersController#search as JSON
2013-09-05T07:30:58.428178+00:00 app[web.1]: Parameters: {"user"=>"heeman"}
2013-09-05T07:30:58.428178+00:00 app[web.1]: Completed 200 OK in 200ms (Views: 0.1ms | ActiveRecord: 7.5ms)

这是通过我的 ios 客户端的日志

2013-09-05T07:35:35.766319+00:00 app[web.1]: 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[router]: 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[web.1]: Processing by UsersController#search as JSON
2013-09-05T07:35:35.773667+00:00 app[web.1]: Parameters: {"user"=>{}}
2013-09-05T07:35:35.773667+00:00 app[web.1]: Completed 200 OK in 3ms (Views: 0.2ms | ActiveRecord: 1.2ms)

如果您在日志中看到,该 url 确实显示了参数 method=GET path=/search.json?user=heeman 但是 Parameters: {"user"=>{ }}

最佳答案

删除请求中的 Content-type 设置。您不包括任何数据主体,只包括参数,因为这是 GET,而不是 PUT 或 POST。 GET 请求不应包含 Content-type(它们应该只包含 Accept),但这不是硬性要求,它的影响取决于服务器处理请求。

关于ios - 如果通过 iOS 客户端发送请求,则无法识别参数,Curl 相同的 url 按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18629605/

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