I'm working on an iOS app that was implement in RestKit .10 and am updating it to .20. One of the post requests the app makes has a block of data as a parameter. Previously, the app encoded this use RKParam setData:MIMEType:forParam; I'm looking for the .20 version of this for adding a parameter to a NSMutableDictionary parameter.
Old code:
NSString *logString; // loaded up elsewhere
NSData *textFileContentsData = [logString dataUsingEncoding:NSUTF8StringEncoding];
RKParams *params = [RKParams params];
[params setData: textFileContentsData MIMEType:#"text/plain" forParam:#"log_file"];
New code:
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:???? forKey:#"log_file"]; // not sure how to get string here.
I have tried putting the textFileContentsData NSData object in for ???? but the data arrives in binary, which is not what is required. I need to figure out how to retain the text/plain MIMEType.
TIA
Janene
From your description I wouldn't use RestKit for this as there is no mapping going on, I'd use AFNetworking instead (vehicular is included in RestKit so you already have full access).
Using RestKit, you would use the object manager to create a request to send, something like:
NSMutableURLRequest *request =
[objectManager multipartFormRequestWithObject:nil
method:RKRequestMethodPOST
path:#"..."
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:...
name:#"..."];
}];
Then you could use RKObjectRequestOperation to process the request. But I'm not convinced this is exactly what you need if you just want to set the mime type (just a header) and the post data, both of which can be done directly on NSMutableURLRequest.
Related
i'm using AFNetworking 2.+ in my iOS app,
my server uses JSON Vulnerability Protection.
that makes my requests to the server "Half working".
meaning, i do get code 200 for my requests, but the request fails.
i can't parse the json.
I'm using AFHTTPRequestOperationManager and i set his Serializers like that:
[self setResponseSerializer:[AFJSONResponseSerializer serializer]];
[self setRequestSerializer:[AFJSONRequestSerializer serializerWithWritingOptions:NSJSONWritingPrettyPrinted]];
i also tried that:
[self setResponseSerializer:[AFJSONResponseSerializer serializer]];
[self setRequestSerializer:[AFJSONRequestSerializer serializer]];
didnt worked as well.
as far as i can tell, JSON Vulnerability Protection adds ")]}'," before the real json, as shown here in angular docs
JSON Vulnerability Protection A JSON vulnerability allows third party
website to turn your JSON resource URL into JSONP request under some
conditions. To counter this your server can prefix all JSON requests
with following string ")]}',\n". Angular will automatically strip the
prefix before processing it as JSON.
For example if your server needs to return:
['one','two']
which is vulnerable to attack, your server can return:
)]}', ['one','two']
is there any way to handle that with the provided tools AFNetworking gives me?
Should i use a custom AFJSONRequestSerializer?
Thanks,
Shahar.
Answering my own...
i implemented
and added that method:
-(id)responseObjectForResponse:(NSURLResponse *)response data:(NSData *)data error:(NSError *__autoreleasing *)error{
NSString * jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (jsonString && [jsonString hasPrefix:#")]}',"]) {
jsonString = [jsonString substringFromIndex:#")]}',".length];
}
NSData *newData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
return [NSJSONSerialization JSONObjectWithData:newData options:0 error:nil];
}
That fixed that.
I am new to AFNetworking and I know how to pass URL parameters. But how would I pass headers into the same call.
I am also subclassing my AFHTTPSessionManager
See my code below:
- (void)getExpenses:(NSString *)page
success:(void (^) (NSArray *myExpenses))success
failure:(RequestFailureBlock)failure
{
NSString *resourceURL = [NSString stringWithFormat:#"%#/expenses/", APIBaseURLString];
NSDictionary *parameters = #{#"page":page, #"Authorization": APIAuthorization};
[self getExpenses:resourceURL parameters:parameters success:success failure:failure];
}
setAuthorizationHeaderFieldWithToken is deprecated due to servers having different requirements about how the access token is sent (token, bearer, etc)
michaels answer otherwise is correct, use
[self.requestSerializer setValue:#"Some-Value" forHTTPHeaderField:#"Header-Field"];
or
[self.requestSerializer setAuthorizationHeaderFieldWithUsername:#"" password:#""];
for basic auth
You set header values on the requestSerializer property of AFHTTPSessionManager:
[self.requestSerializer setValue:#"Some-Value" forHTTPHeaderField:#"Header-Field"];
EDIT:
It looks like you're trying to set authorization; there is a method for that too:
[self.requestSerializer setAuthorizationHeaderFieldWithUsername:#"" password:#""];
// OR
[self.requestSerializer setAuthorizationHeaderFieldWithToken:#""];
If you need to set the Content-Type header, see this SO answer on how to do that
I followed the suggested solution at AFNetworking 2.0 add headers to GET request to specify custom headers for the request with the following code snippet:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:someID forHTTPHeaderField:#"some_id"];
NSDictionary *parameters = #{#"id": user.id, #"birthday": user.birthday};
[manager POST:[NSString stringWithFormat:#"%#/user_create",BaseURLString] parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
if (responseObject[#"error"])
{
NSLog(#"REST User Create Response Error: %#", responseObject[#"error"]);
}
else
{
[self saveUserDetails:responseObject];
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"REST User Create Error: %#", error);
}];
But what happens when this gets executed is I get an error in the response from the API stating all my parameters are missing. This same block of code used to work before (without setting the custom header and when the API didn't require them originally).
Does anybody know how to properly set both custom headers and POST parameters?
Thanks,
Nino
Your code looks fine to me.
A few things to try:
Try using AFNetworkActivityLogger to see what's actually being sent (set it to AFLoggerLevelDebug.) You can also use a web proxy like Charles or a protocol analyzer like Wireshark.
If you determine the data is not being sent properly, set a breakpoint in [AFURLRequestSerialization -requestBySerializingRequest:withParameters:error:]. This is where your HTTP headers and parameters are added to the URL request. The method is pretty straightforward; you should be able to step through and watch as stuff is added to the request and determine why it gets skipped.
NOTE: AFURLRequestSerialization.m contains multiple subclasses of AFURLRequestSerialization. Set a breakpoint in the super implementation, as well as in the AFJSONRequestSerializer implementation.
Examples that could cause this behavior:
parameters is nil.
you've added POST to HTTPMethodsEncodingParametersInURI but
your API is not prepared to handle parameters appended to a URL on a POST request, or
the queryStringSerialization block is nil AND queryStringSerializationStyle is set to something other than AFHTTPRequestQueryStringDefaultStyle.
NSJSONSerialization can't handle your parameters dictionary
One side note (unrelated to your problem), if you use AFHTTPRequestOperationManager's initWithBaseURL: method, and keep a strong reference to your manager, you won't have to do that [NSString -stringWithFormat:] stuff to construct your URL.
I discovered that I should have set my request serializer to the following instead since my API doesn't require JSON-formatted parameters.
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
Hope this helps others dealing with the same situation as mine.
I'm trying to post an image with a parameter (categories) that is an array. If I leave this parameter out, I can submit fine. However, if I leave it in, my server throws an error because the array value doesn't seem to be getting serialized correctly.
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:#{
#"auth_token" : authToken,
#"title" : title,
#"categories" : #[1,2,3]
}];
return [[MyHTTPSessionManager sharedManager] POST:path parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:#"image"
fileName:#"image.jpg"
mimeType:#"image/jpeg"];
} success:nil failure:nil];
Looking at my server logs, this is how categories is coming across:
Parameters: {"categories"=>"(\n 1,\n 2,\n 3\n)", … }
And I need it to look like:
Parameters: {"categories"=>[1,2,3], … }
I've tried setting my instance of AFHTTPSessionManager's requestSerializer to [AFJSONRequestSerializer serializer] but that doesn't seem to help.
How can I properly POST an image + parameters? I'd like to avoid falling back to AFHTTPRequestOperation.
Thanks!
Try upgrading to the newest version of AFNetworking. I believe this was fixed in github.com/AFNetworking/AFNetworking/issues/1388, which I think was rolled into AFNetworking 2.0.1. If the newest version doesn't work, just pull the latest commit.
It seems for batch requests, all the parameters are escaped as parts of relative_url, if omit_response_on_success is set to #(false), app will crash with this message: -[__NSCFNumber length]: unrecognized selector
NSDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys: #(false), #"omit_response_on_success", nil];
FBRequest *request1 = [FBRequest requestWithGraphPath:self.graphPath
parameters:parameters
HTTPMethod:nil];
[newConnection addRequest:request1 completionHandler:handler batchEntryName:#"entryName"];
If the graphPath is set to #"me/home?omit_response_on_success=0", these will be no output from this operation. Any ideas?
Yes, this option is currently not supported by the SDK as-is, be sure to file a feature request on https://developers.facebook.com/bugs for this.
That should not be a parameter but a key-value in the JSON body of the request, as noted in the docs. I believe the question is rather how to set that key-value in the iOS SDK since we don't have access to the body of the request. From what I could tell there is no way to do it, but I'm not sure if it's a bug.
It's very annoying that Facebook doesn't allow us to set this flag using the iOS SDK. I spent hours trying to figure out a way and this is a little hack I came up with. It should be relatively safe. Just use the RSFBRequestConnection instead of FBRequestConnection:
#interface RSFBRequestConnection : FBRequestConnection
#end
#implementation RSFBRequestConnection
- (NSMutableURLRequest *)urlRequest
{
NSMutableURLRequest *request = [super urlRequest];
NSData *body = request.HTTPBody;
NSString *bodyStr = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(#"%#", bodyStr);
NSString *fixed = [bodyStr stringByReplacingOccurrencesOfString:#"\"relative_url\"" withString:#"\"omit_response_on_success\":false,\"relative_url\""];
request.HTTPBody = [fixed dataUsingEncoding:NSUTF8StringEncoding];
return request;
}
#end