Thursday 2 November 2017

ios - How to sent data in variable via POST and AFNetworking?

style="font-weight: bold;">

Answer



style="font-weight: bold;">

Answer






I use this
method:



NSDictionary *params =
@{@"email" : email,
@"password" : pass
};
NSString*
HOST_URL = @"http://server.com:8080";

AFHTTPClient *httpClient =
[[AFHTTPClient alloc] initWithBaseURL:[NSURL
URLWithString:HOST_URL]];
NSMutableURLRequest *request = [httpClient
requestWithMethod:@"POST"
path:@"/login"


parameters:params];

AFHTTPRequestOperation *operation =
[[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient
registerHTTPOperationClass:[AFHTTPRequestOperation
class]];

[operation
setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{

NSLog(@"Response: %@", [[NSString alloc]
initWithData:responseObject encoding:NSUTF8StringEncoding]);
NSDictionary
*response = (NSDictionary*)[NSJSONSerialization
JSONObjectWithData:operation.responseData options:kNilOptions
error:nil];


//HERE YOU HAVE A RESPONSE (your server
answer)
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@",
error.localizedDescription);
}];

[operation
start];


And I have
response:




{"Result":"fail","Message":"unexpected
end of JSON
input","Data":null}


My
requirements is to send login and password via POST in variable "data", but how can I
sent this variable on my server if I send request via text in dictionary here?


style="font-weight: bold;">

Answer




NSDictionary *params =
@{@"email" : email,
@"password" : pass
};
NSString*
HOST_URL =
@"http://server.com:8080";


AFHTTPRequestOperationManager
*operationManager = [AFHTTPRequestOperationManager manager];
[operationManager
POST:HOST_URL parameters:params success:^(AFHTTPRequestOperation *operation, id
responseObject){

// Enter what happens here if
successsful.

}failure:^(AFHTTPRequestOperation *operation, NSError
*error){

// Enter what happens here if failure
happens



}


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...