Home > Mobile >  AFNetwoking post request body when raw requirements have been unsuccessful, using a postman can succ
AFNetwoking post request body when raw requirements have been unsuccessful, using a postman can succ

Time:09-30

AFHTTPSessionManager * session=[AFHTTPSessionManager manager];

The session. RequestSerializer=[AFJSONRequestSerializer serializer];
[session. RequestSerializer setValue: @ "charset=utf-8; application/json" forHTTPHeaderField: @ "content-type"].

The session. ResponseSerializer=[AFJSONResponseSerializer serializer];

[session POST: URLString parameters: the parameters progress: ^ (NSProgress * _Nonnull uploadProgress) {

} success: ^ (NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
If (responseObject) {

}
} failure: ^ (NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

}];

CodePudding user response:

AFJSONRequestSerializer. This is the need to request the json format, if not can use the other

CodePudding user response:

NSData * JSONdata=https://bbs.csdn.net/topics/nil;
If (parameters) {
JSONdata=https://bbs.csdn.net/topics/[NSJSONSerialization dataWithJSONObject: the parameters options: NSJSONWritingPrettyPrinted error: nil];
}



InitWithSessionConfiguration AFHTTPSessionManager * manager=[[AFHTTPSessionManager alloc] : [NSURLSessionConfiguration defaultSessionConfiguration]].

NSMutableURLRequest * request=[[AFHTTPRequestSerializer serializer] requestWithMethod: methodString URLString: urlStr parameters: nil error: nil];
Request. TimeoutInterval=TIMEOUT;
[request setValue: @ "application/json forHTTPHeaderField:" @ "content-type"].
Request. CachePolicy=NSURLRequestReloadIgnoringCacheData;


[request setValue: @ "application/json forHTTPHeaderField:" @ "content-type"].
//set the body
[request setHTTPBody: JSONdata];

Manager. ResponseSerializer. AcceptableContentTypes=[NSSet setWithObjects:
"@" application/json,
"@" text/HTML,
@ "text/json,"
@ "text/plain",
@ "text/javascript," nil];

NSURLSessionDataTask * task=nil;

Task=[manager dataTaskWithRequest: request uploadProgress: nil downloadProgress: nil completionHandler: ^ (NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {

If (error) {
If (failureBlock) {
FailureBlock (responseObject, error);
}
} else {
If (successBlock) {
SuccessBlock (responseObject);
}
}
}];
[task resume];
  •  Tags:  
  • iOS
  • Related