Nic*_*ick 13 rest objective-c ios afnetworking
我正在尝试使用AFNetworking在CouchDB服务器中创建附件的HTTP PUT请求.服务器需要HTTP主体中的base64编码字符串.如何在不将HTTP正文作为键/值对发送的情况下发出此请求AFNetworking?
我开始看这个方法:
- (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
Run Code Online (Sandbox Code Playgroud)
但这里的参数应为:NSDictionary.我只想在HTTP正文中发送base64编码的字符串,但不与密钥相关联.有人能指出我使用的适当方法吗?谢谢!
Aar*_*ger 11
Hejazi的答案很简单,应该很有效.
如果由于某种原因,您需要非常具体地针对一个请求 - 例如,如果您需要覆盖标题等 - 您还可以考虑构建自己的NSURLRequest.
这是一些(未经测试的)示例代码:
// Make a request...
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL];
// Generate an NSData from your NSString (see below for link to more info)
NSData *postBody = [NSData base64DataFromString:yourBase64EncodedString];
// Add Content-Length header if your server needs it
unsigned long long postLength = postBody.length;
NSString *contentLength = [NSString stringWithFormat:@"%llu", postLength];
[request addValue:contentLength forHTTPHeaderField:@"Content-Length"];
// This should all look familiar...
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postBody];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:success failure:failure];
[client enqueueHTTPRequestOperation:operation];
Run Code Online (Sandbox Code Playgroud)
NSData的类别方法base64DataFromString是可以在这里找到.
| 归档时间: |
|
| 查看次数: |
13777 次 |
| 最近记录: |