在 iOS 中使用“Body Requests”发送 NSMutableURLRequest 请求
Posted
技术标签:
【中文标题】在 iOS 中使用“Body Requests”发送 NSMutableURLRequest 请求【英文标题】:Sending a NSMutableURLRequest request with “Body Requests” In iOS 【发布时间】:2015-05-19 06:38:15 【问题描述】:我目前正在使用 Google 日历 API 并允许用户与与会者一起发布活动,但是现在我应该发送一个包含“请求正文”的 POST
方法。
这是我应该发送的请求:
https://www.googleapis.com/calendar/v3/calendars/<MyCalendorID>/events/<MyEventID>
Request Body :
"end":
"date": "2015-05-19"
,
"start":
"date": "2015-05-20"
,
"attendees": [
"email": "nai.bhavesh782@gmail.com"
]
我很肯定我在 [request setHTTPBody]
做错了什么,但这是我唯一能想到的。
请帮忙
【问题讨论】:
不确定这是否有帮助,但看看:***.com/questions/29669296/… 同样的问题,你找到解决方法了吗? ***.com/questions/31585403/… @János 我发布了我的答案检查它:) 【参考方案1】: NSMutableDictionary *RequestDict = [[NSMutableDictionary alloc] init];
BOOL *remind_bool = false;
NSMutableDictionary *remindDict = [[[NSMutableDictionary alloc]init] autorelease];
[remindDict setObject:[NSNumber numberWithBool:remind_bool] forKey:@"useDefault"];
[RequestDict setObject:[eventInfoDict objectForKey:@"end"] forKey:@"end"];
[RequestDict setObject:[eventInfoDict objectForKey:@"start"] forKey:@"start"];
[RequestDict setObject:AttendeesArr forKey:@"attendees"];
[RequestDict setObject:_txt_eventname.text forKey:@"summary"];
[RequestDict setObject:_txt_desc.text forKey:@"description"];
[RequestDict setObject:_txt_location.text forKey:@"location"];
[RequestDict setObject:remindDict forKey:@"reminders"];
[RequestDict setObject:@"tentative" forKey:@"status"];
[RequestDict retain];
NSLog(@"%@",RequestDict);
-(void)callAPIfortesting:(NSString *)apiURL withHttpMethod:(HTTP_Method)httpMethod
postParameterNames:(NSArray *)params
postParameterValues:(NSArray *)values postParameterDict:(NSDictionary *)RequestDict
// Check if the httpMethod value is valid.
// If not then notify for error.
if (httpMethod != httpMethod_GET && httpMethod != httpMethod_POST && httpMethod != httpMethod_DELETE && httpMethod != httpMethod_PUT)
[self.gOAuthDelegate errorOccuredWithShortDescription:@"Invalid HTTP Method in API call" andErrorDetails:@""];
else
// Create a string containing the API URL along with the access token.
NSString *urlString = [NSString stringWithFormat:@"%@?access_token=%@", apiURL,[_accessTokenInfoDictionary objectForKey:@"access_token"]];
// Create a mutable request.
NSUserDefaults *UserDefaults = [NSUserDefaults standardUserDefaults];
[UserDefaults setObject:[_accessTokenInfoDictionary objectForKey:@"access_token"] forKey:@"AccessToken"];
[UserDefaults synchronize];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// Depending on the httpMethod value set the respective property of the request object.
switch (httpMethod)
case httpMethod_GET:
[request setHTTPMethod:@"GET"];
break;
case httpMethod_POST:
[request setHTTPMethod:@"POST"];
break;
case httpMethod_DELETE:
[request setHTTPMethod:@"DELETE"];
break;
case httpMethod_PUT:
[request setHTTPMethod:@"PUT"];
break;
default:
break;
// In case of POST httpMethod value, set the parameters and any other necessary properties.
if (httpMethod == httpMethod_POST)
// A string with the POST parameters should be built.
// Create an empty string.
NSString *postParams = @"";
// Iterrate through all parameters and append every POST parameter to the postParams string.
for (int i=0; i<[params count]; i++)
postParams = [postParams stringByAppendingString:[NSString stringWithFormat:@"%@=%@",
[params objectAtIndex:i], [values objectAtIndex:i]]];
// If the current parameter is not the last one then add the "&" symbol to separate post parameters.
if (i < [params count] - 1)
postParams = [postParams stringByAppendingString:@"&"];
NSLog(@"%@",postParams);
if([NSJSONSerialization isValidJSONObject:RequestDict])
// Convert the JSON object to NSData
NSData * httpBodyData = [NSJSONSerialization dataWithJSONObject:RequestDict options:0 error:nil];
// set the http body
[request setHTTPBody:httpBodyData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSLog(@"%@",request);
// Make the request.
[self makeRequest:request];
【讨论】:
以上是关于在 iOS 中使用“Body Requests”发送 NSMutableURLRequest 请求的主要内容,如果未能解决你的问题,请参考以下文章