Objective request c do not send data

A good day. I am trying to send simple mail data to a server. This is the code how I do it.

 -(void)makeRequest:(NSString*)stringParameters{
    NSError *error;

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURL *url = [NSURL URLWithString:@"http://vaenterprises.webatu.com/Authentication.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];

    [request addValue:@"application/text" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/text" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
     NSString* postData = @"tag=hello&username=yo&something=something";
    [request setHTTPBody:postData];



    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        [self parseJson:data];
    }];

    [postDataTask resume];


}

It looks great until I respond to the whole post from the php side, like this

echo json_encode($_POST);

and I will print the result in iOS like this

-(void)parseJson:(NSData*) data{
    NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSError *jsonError = nil;
    NSDictionary* jsonObject= [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
    NSLog(@"%@",myString);
}

this problem is that I get an empty string..if this means that the post data is not being sent, and this is a 10000 percent c-side object problem, and I don’t know why it is the way it is in this method, we received only setHttpBody with the actual string containing the key and value, separated and but the data is not sent, as you can see. So what am I doing wrong? Please tell someone

+4
source share
1

Http NSData.

NSString* stringData = @"tag=hello&username=yo&something=something";
NSData* data = [stringData dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
0

Source: https://habr.com/ru/post/1626737/


All Articles