【问题标题】:How to send GET request in Objective C containing JSON data to a url如何在包含 JSON 数据的 Objective C 中将 GET 请求发送到 url
【发布时间】:2015-01-29 20:27:20
【问题描述】:

我可以使用以下代码访问我的 API url 并接收 json 数据,但是我需要使用以下参数和值指定要请求的数据。我该怎么做?

参数: q 值:追逐

参数:页面值:1

NSMutableURLRequest *request3 = [[NSMutableURLRequest alloc] init];
[request3 setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request3 setURL:[NSURL URLWithString:@"https://MYWEBSITE.com/api/v1/intuit/institutions"]];
[request3 setHTTPMethod:@"GET"];
NSURLResponse *requestResponse3;
NSData *requestHandler3 = [NSURLConnection sendSynchronousRequest:request3 returningResponse:&requestResponse3 error:nil];
NSError *err3;
NSMutableDictionary *json3 = [NSJSONSerialization JSONObjectWithData:requestHandler3 options:kNilOptions error:&err3];
NSLog(@"%@", json3);

【问题讨论】:

标签: objective-c json api get request


【解决方案1】:

刚刚想通了,所有的代码都是一样的,你只需要在url末尾添加以下内容来更改url地址 ?参数=值&参数=值

NSMutableURLRequest *request3 = [[NSMutableURLRequest alloc] init];
[request3 setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request3 setURL:[NSURL URLWithString:@"https://MYWEBSITE.com/api/v1/intuit/institutions?q=chase&page=2"]];
[request3 setHTTPMethod:@"GET"];
NSURLResponse *requestResponse3;
NSData *requestHandler3 = [NSURLConnection sendSynchronousRequest:request3 returningResponse:&requestResponse3 error:nil];
NSError *err3;
NSMutableDictionary *json3 = [NSJSONSerialization JSONObjectWithData:requestHandler3 options:kNilOptions error:&err3];
NSLog(@"%@", json3);

【讨论】:

  • 没关系,但是Content-Type 不是application/json,所以你不应该设置那个标题。如果需要,可以将Accept 设置为application/json,但不要设置Content-Type
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 2021-05-23
  • 2018-02-28
  • 2023-04-02
  • 2015-03-15
  • 1970-01-01
相关资源
最近更新 更多