Posting JSON data using AFNetworking 2.0
after searching docs and trying out some codes I got following as an example
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *params = @ {@"user" :txtUserName, @"pwd" :txtPwd };
[manager POST:URL_SIGNIN parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
}
failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
Also don't forget to set response header type in the server script as Application/json.
How to POST data using AFNetworking 2.0?
Does your web service expect the parameters to be formatted as JSON? If so, before you invoke the POST
method, you need to tell the manager
to use a JSON requestSerializer
, i.e. AFJSONRequestSerializer
:
manager.requestSerializer = [AFJSONRequestSerializer serializer];
By default, AFNetworking assumes you want to use AFHTTPRequestSerializer
(i.e. a request with a Content-Type
of application/x-www-form-urlencoded
).
send JSON object in POST request AFNetworking , iOS
@interface AFJSONRequestSerializer : AFHTTPRequestSerializer
AFJSONRequestSerializer
is a subclass of
AFHTTPRequestSerializerthat encodes parameters as JSON
usingNSJSONSerialization
.
add this AFJSONRequestSerializer
is used for send JSON not a Raw Data add this and try once
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
e.g
NSString *string = [NSString stringWithFormat:@"%@?post_user_info",BaseURLString];
NSString *escapedPath = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
/************** karthik Code added ******************/
AFHTTPSessionManager* manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
/************** karthik Code added ******************/
NSDictionary *params = @{@"1": @"hello world",
@"2": @"my@you.com",
@"3": @"newworldorder"};
[manager POST:escapedPath parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
NSLog(@"%@",responseObject);
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
NSLog(@"json == %@", json);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"%@", [error localizedDescription]);
}];
Update Params
NSDictionary *params = @{@"1": @"hello world",
@"2": @"my@you.com",
@"3": @"newworldorder"};
NSMutableDictionary *modify = [NSMutableDictionary new];
[modify setObject:params forKey:@"input_values"];
you get output of
AFNetworking Send JSON as a parameter for POST request
try this
directly pass loginDict
in your call and check
manager.requestSerializer = [AFJSONRequestSerializer serializer];
/*
[manager.requestSerializer setTimeoutInterval:20];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
*/
[manager POST:baseUrlLogin parameters: loginDict
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
AFNetworking 2.0 post not working well
You returned JSON data as a response, but your assumption that you set Content-Type is incorrect. Create a response instance with the mimetype set to 'application/json'.
from flask import request, json
@app.route('/test', methods=['POST'])
def test():
data = json.dumps(request.get_json())
resp = app.response_class(data, mimetype='application/json')
return resp
Related Topics
What's the Difference Between "Architectures" and "Valid Architectures" in Xcode Build Settings
iOS Blocks and Strong/Weak References to Self
Posting Video on Instagram Using Hooks
Facebook Sdk Login Never Calls Back My Application on iOS 9
Defaultcalendarfornewevents Failed
Xmppframework - How to Create a Muc Room and Invite Users
How to Use a Uisegmentedcontrol to Switch Views
iOS 8 Auto Cell Height - Can't Scroll to Last Row
Change Default Icon for Moving Cells in Uitableview
React/Rctbridgemodule.H' File Not Found
How to Underline a Uilabel in Swift
Custom Uitableviewcell Selection Style