title: set post request body to form-data objective-c
body: <p>I am trying to send <code>form-data</code> in a <strong>post request body</strong> but the service is returning null so either the method i am using is wrong (it was generated by postman) or the image encoding is different, i will show you how the image is supposed to be encoded and how i am encoding it through the screenshots below besides the code i use</p>
  <pre><code>NSString* imgName = [self randomCode];
      NSString* postFile = [NSString stringWithFormat:@&quot;%@%@.png&quot;,imgName,userData.userID];
      
      NSDictionary *headers = @{ @&quot;content-type&quot;: @&quot;multipart/form-data; boundary=----WebKitFormBoundaryAt4JdBdihQo7lrrf&quot;,
                                 @&quot;authorization&quot;: [NSString stringWithFormat:@&quot;Bearer %@&quot;,userData.accessToken ],
                                 @&quot;moduleid&quot;: @&quot;441&quot;,
                                 @&quot;tabid&quot;: @&quot;78&quot;,
                                 @&quot;cache-control&quot;: @&quot;no-cache&quot;,
                                 @&quot;postman-token&quot;: @&quot;1482fe03-076b-aa92-5565-125212592762&quot; };
      NSArray *parameters = @[ @{ @&quot;name&quot;: @&quot;folder&quot;, @&quot;value&quot;: userData.userFolder },
                               @{ @&quot;name&quot;: @&quot;filter&quot;, @&quot;value&quot;: @&quot;bmp,gif,jpeg,jpg,jpe,png&quot; },
                               @{ @&quot;name&quot;: @&quot;overwrite&quot;, @&quot;value&quot;: @&quot;true&quot; },
                               @{ @&quot;name&quot;: @&quot;postfile&quot;, @&quot;fileName&quot;: postFile } ];
      NSString *boundary = @&quot;----WebKitFormBoundaryAt4JdBdihQo7lrrf&quot;;
      
      NSMutableString *body = [NSMutableString string];
      for (NSDictionary *param in parameters) {
          [body appendFormat:@&quot;--%@\r\n&quot;, boundary];
          if (param[@&quot;fileName&quot;]) {
              [body appendFormat:@&quot;Content-Disposition: form-data; name=\&quot;%@\&quot;; filename=\&quot;%@\&quot;\r\n&quot;, param[@&quot;name&quot;], param[@&quot;fileName&quot;]];
              [body appendFormat:@&quot;Content-Type: %@\r\n\r\n&quot;, @&quot;image/png&quot;];
              
              NSString* imgString = [self encodeToBase64String:self.teamImage];
              [body appendFormat:@&quot;%@&quot;, imgString];
  
              ;
          } else {
              [body appendFormat:@&quot;Content-Disposition: form-data; name=\&quot;%@\&quot;\r\n\r\n&quot;, param[@&quot;name&quot;]];
              [body appendFormat:@&quot;%@\n&quot;, param[@&quot;value&quot;]];
          }
      }
      [body appendFormat:@&quot;\r\n--%@--\r\n&quot;, boundary];
      NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];
      
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@&quot;http://172.16.0.216/API/internalservices/fileupload/postfile&quot;]
                                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                         timeoutInterval:10.0];
      [request setHTTPMethod:@&quot;POST&quot;];
      [request setAllHTTPHeaderFields:headers];
      [request setHTTPBody:postData];
      
      NSURLSession *session = [NSURLSession sharedSession];
      NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                                  completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                      if (error) {
                                                          NSLog(@&quot;%@&quot;, error);
                                                      } else {
                                                          NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                          NSLog(@&quot;%@&quot;, httpResponse);
                                                      }
                                                  }];
      [dataTask resume];
  
  
  - (NSString *)encodeToBase64String:(UIImage *)image {
      NSData * data = [UIImagePNGRepresentation(image) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
      return [NSString stringWithUTF8String:[data bytes]];
  }
  </code></pre>
  <p>here is how the image should look like when the server receives is :
  <a href="https://i.stack.imgur.com/NYAbk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NYAbk.png" alt="enter image description here" /></a>
  and here is how the server receive it from me :
  <a href="https://i.stack.imgur.com/Wordb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Wordb.png" alt="enter image description here" /></a></p>
  <p>please tell me what i am doing wrong</p>
  <p>thanks</p>