AFNetworking- post request- add simple text to body

How do i add a simple string (no JSON or any other format), to a post request using AFNetworking?
The best i’ve already succeeded was concat with ‘=’.

And this:

  • AFNetworking - How to setup requests to be retried in the event of a timeout?
  • AFHTTPSessionManager to use SOAP based service
  • How to send multiple parameterts to PHP server in HTTP post
  • How to set HTTP request body using AFNetwork's AFHTTPRequestOperationManager?
  • UITableViewCell height resize when image is downloaded
  • Switching from http to https. Invalid certificate
  •  NSURLRequest* request =[myServer multipartFormRequestWithMethod:@"POST" path:@"http://my.server.com" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSData *tmp_data = [NSString stringWithFormat:@"%@", @"my_string!"];
        [formData appendPartWithHeaders:nil body:tmp_data];
    }];
    

    Thanks in advance!

    Solutions Collect From Internet About “AFNetworking- post request- add simple text to body”

    As simple as is, This should be the answer:

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://www.my.server.com"]];
    [request setHTTPMethod:@"POST"];
    
    //set headers
    NSString *contentType = @"text/xml";
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    [request addValue:@"any-value" forHTTPHeaderField: @"User-Agent"];
    
    //create the body
    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[@"my_body_string!" dataUsingEncoding:NSUTF8StringEncoding]];
    
    //post  
    [request setHTTPBody:postBody];
    

    From here, do what you want with the http request (i used with AFNetworking for sending).

    Cheers!