I am trying to connect to an Api but it is using x-www-form-urlencoded ...and i am using this code .Plz tell me the correct method
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = nil;
manager.securityPolicy = securityPolicy;
[manager POST:[NSString stringWithFormat:#"#"%#%#phone=%#&password=%#",BASE_URL,API_LOGIN,phone,password]
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
successBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Server Error : %#", operation.responseString);
errorBlock(error);
}];
}
I think you should try to set the http header for 'x-www-form-urlencoded' to communicate with your backend:
[manager.requestSerializer setValue:#"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
If this does not fix your errors, then try to validate your json object.
You could also have a look to this question: AFNetworking 3 x-www-form-urlencoded post data
I had an application in which I am using Afnetnetworking3.0 for web service calls like this.
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
NSURL * urlStr = [NSURL URLWithString:path];
NSDictionary *parameters = #{#"ID":#"fdfdfd",#"No":#"1414"};
NSLog(#"%#",parameters);
[manager POST:urlStr.absoluteString
parameters:parameters
progress:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"Error: %#", error);
}];
But it is adding some spaces in parameters dictionary and making the URL not found. Can anybody tell me where I am going wrong ?
Try like this :
NSString *Loginurl = [NSString stringWithFormat:#"http://Enter your url here"];
NSDictionary *params = #{
#"ID":#"fdfdfd",
#"No":#"1414"
};
NSLog(#"Sent parameter to server 1 : %#",params);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
[policy setValidatesDomainName:NO];
[policy setAllowInvalidCertificates:YES];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json", #"text/javascript",#"text/html", nil];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json",#"text/html",nil];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json",#"text/plain",nil];
[manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"Response from server 1 : %#", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
[self getdata:responseObject];
}
failure:^(NSURLSessionTask *operation, NSError *error)
{
NSLog(#"Error: %#", error);
}];
}
-(void)getdata:(NSDictionary*)RegisterData
{
}
check the log Sent parameter to server 1... and check your url is correct or not?
Im using salesforce live agent rest api in ios app, trying to request chat but always fail with 400 request is not a valid ChasitorInit
NSDictionary *parameters =#{ #"SessionId" :self.sessionId,
#"OrganizationId" :ORG_ID,
#"deploymentId" :DEPLOYEMENT_ID,
#"buttonId" :BUTTON_ID,
#"userAgent" :USER_AGENT,
#"language" :LANG,
#"screenResolution" :SCREEN_RES,
#"visitorName" :#"Test Visitor",
#"prechatDetails" :#[],
#"prechatEntities" :#[],
#"receiveQueueUpdates":#"true",
#"isPost" :#"true"
};
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[serializer setValue:#"application/json" forHTTPHeaderField:#"Accept"];
manager.requestSerializer = serializer;
[manager.requestSerializer setValue:self.sessionKey forHTTPHeaderField:X_LIVEAGENT_SESSION_KEY];
[manager.requestSerializer setValue:#"null" forHTTPHeaderField:X_LIVEAGENT_AFFINITY];
[manager.requestSerializer setValue:#"1" forHTTPHeaderField:X_LIVEAGENT_SEQUENCE];
[manager.requestSerializer setValue:API_V forHTTPHeaderField:X_LIVEAGENT_API_VERSION];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:ChasitorInit_path parameters:parameters progress:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject
options:kNilOptions
error:&error];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"Error: %#", error);
}];
Im using Afnetworking v3 ios8
The request body should be
{
"sessionId": "8f1cfb18-fbd1-4487-90d-e98c0a84e165",
"organizationId": "00D58000000OwMB",
"deploymentId": "57258000000L1mj",
"buttonId": "57358000000L24F",
"userAgent": "",
"language": "en-US",
"screenResolution": "1900x1080",
"visitorName": "Test Visitor",
"prechatDetails": [],
"prechatEntities": [],
"receiveQueueUpdates": true,
"isPost": true
}
What im i doing wrong ? Plz correct me !!
400 Bad request means, you didn't form the request the way server expects.
Check the server logs for whats the issue...
May be it needs data in another form..
and try to check that,you are not passing null value to server..
Try like that:
NSString *Loginurl = [NSString stringWithFormat:#"http://Enter your URL Here"];
NSDictionary *params = #{ #"SessionId" :self.sessionId,
#"OrganizationId" :ORG_ID,
#"deploymentId" :DEPLOYEMENT_ID,
#"buttonId" :BUTTON_ID,
#"userAgent" :USER_AGENT,
#"language" :LANG,
#"screenResolution" :SCREEN_RES,
#"visitorName" :#"Test Visitor",
#"prechatDetails" :#{},
#"prechatEntities" :#{},
#"receiveQueueUpdates":#"true",
#"isPost" :#"true"
};
NSLog(#"Sent parameter to server : %#",params);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json", #"text/javascript",#"text/html", nil];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json",#"text/html",nil];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json",#"text/plain",nil];
[manager.requestSerializer setValue:self.sessionKey forHTTPHeaderField:X_LIVEAGENT_SESSION_KEY];
[manager.requestSerializer setValue:#"null" forHTTPHeaderField:X_LIVEAGENT_AFFINITY];
[manager.requestSerializer setValue:#"1" forHTTPHeaderField:X_LIVEAGENT_SEQUENCE];
[manager.requestSerializer setValue:API_V forHTTPHeaderField:X_LIVEAGENT_API_VERSION];
AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
[policy setValidatesDomainName:NO];
[policy setAllowInvalidCertificates:YES];
[manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"Response from server : %#", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
_str=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"%#",_str);
[self getdata:responseObject];
} failure:^(NSURLSessionTask *operation, NSError *error)
{
NSLog(#"Error: %#", error);
}];
}
#pragma mark-DataFromServer
-(void)getdata:(NSDictionary*)RegisterData
{
}
I got the same error in my app. I resolve it by removing request and response Serializer. Only instantiate manager and called post method with parameter.
reqManager = [AFHTTPRequestOperationManager manager];
[reqManager POST:URLString parameters:parameters progress:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"Error: %#", error);
}];
I am sending GET request to server to getting json response. When i first do it i get some response but then i make changes in the database but still i get the same josn which i was getting.But on browser the json is updated on app it is not updated.I have used below code to make it work but nothing works for me.
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:api_key forHTTPHeaderField:#"Authorization"];
manager.requestSerializer = requestSerializer;
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializer];
responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json", #"text/javascript", #"text/html", nil];
manager.responseSerializer = responseSerializer;
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"json is %#",responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
[self activityIndicator:#"hide"];
NSLog(#"Error: %#", operation.responseString);
}];
I have tried reloadignoringcachedata but this does not work.
This is my code. I set cachePolicy = DontLoad, but it still get data from server not from local cache. Why?
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataDontLoad;
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
manager.responseSerializer = responseSerializer;
[manager GET:#"http://www.github.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"%#", [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];