0

I need to make HTTP POST request, in that request I have to send some text fields and couple of files. Every time I make the request, the files are uploaded, but I get such response. "Success: <656d7074 79>" (aka "empty")

Here is the code, my parameter dictionaries are .

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
AFHTTPRequestOperation *operation = [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    // Append Files
    NSError *error;
    for (NSString *filePathsKey in filesPathsDictionary) {
        [formData appendPartWithFileURL:[NSURL fileURLWithPath:filesPathsDictionary[filePathsKey]] name:filePathsKey error:&error];
        if (error) {
            NSLog(@"%@", error);
        }
    }

    // Text parameters
    for (NSString *textParameterKey in textParamDictionary) {
        [formData appendPartWithFormData:[textParamDictionary[textParameterKey] dataUsingEncoding:NSUTF8StringEncoding] name:textParameterKey];
    }

} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[operation start];

UPDATE: Changed my response serializer to JSON, because I'm expecting JSON manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

And getting this error: Error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

  • `responseObject` represents your server's response. What kind of response are you expecting? – Aaron Brager Oct 23 '15 at 18:52
  • See [my answer here](http://stackoverflow.com/a/17496266/1445366) for some debugging options to get the raw response data – Aaron Brager Oct 23 '15 at 18:55
  • @AaronBrager thanks for the response, I'm expecting json with info. – Nikita Pankiv Oct 23 '15 at 19:00
  • Then why aren't you using the JSON response serializer? – Aaron Brager Oct 23 '15 at 19:05
  • Just in case, as this is an HTTP non secure call, make sure you are disabling the Application Transport Security on your Info.plist. http://stackoverflow.com/a/32894812/760275 – Nicolas S Oct 23 '15 at 19:06
  • @AaronBrager please see update post – Nikita Pankiv Oct 23 '15 at 19:14
  • @NicolasS Added exception to App Transport Security Settings – Nikita Pankiv Oct 23 '15 at 19:15
  • @NikitaPankiv Looks like your server is not sending valid JSON. See the debugging tips I linked to above to inspect the actual response body. – Aaron Brager Oct 23 '15 at 19:26
  • @AaronBrager I tried to POST manually and get nice json (`{"status":1,"data":{"id":"","date":"2015-10-23T13:23:23+0400"}}`) I tried to use your response debugging, but got everything what I knew before, 200 code, and `empty` body – Nikita Pankiv Oct 23 '15 at 19:33
  • The next thing I would look at is your request. How does the request you're making differ from your manual request? – Aaron Brager Oct 23 '15 at 20:58
  • @AaronBrager Here is it `http -f POST "" 'Registration[name]=Nikita' 'Registration[pdf1]@/Users/nikita/test.pdf' 'Registration[pdf2]@/Users/nikita/test.pdf' 'Registration[email]=nikita@gmail.com'` – Nikita Pankiv Oct 23 '15 at 21:10

0 Answers0