3
votes

I am using AFNetworking to make a POST request to a web service

This is the JSON that I need to send-

{
   “LinkedTo” : {
                            “IndividualStakeholder” : “”,
                            “GroupedStakeholder” : “”,
                            “LandParcelStakeholder” : “”,
                            “Communications” : “”,
                            “Team” : “”,
                            “Issue” : “”,
                            “Event” : “”
                        }
   “userId” : 170,
   “projectId”: 12
}

This is the code I am using -

NSString *jsonData = [self JSONString:jsonData1];

The value in NSString *jsonData at this point is

{\"LinkedTo\":{\"IndividualStakeholder\":\"\",\"GroupedStakeholder\":\"\",\"LandParcelStakeholder\":\"\",\"Communications\":\"\",\"Team\":\"\",\"Issue\":\"\",\"Event\":\"\"},\"userId\":170,\"projectId\":4}

NSURL *url = [[NSURL alloc]initWithString:@"https://somedomain.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                             jsonData, @"jsonText" , nil];
NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"https://somedomain.com/api/stakeholders" parameters:params];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
        NSLog(@"Inside the success block %@",JSON);
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
        NSLog(@"json text is: %@", JSON);
        NSLog(@"Request failed with error: %@, %@", error, error.userInfo);
    }];
[operation start];



-(NSString*)JSONString :(NSString*)aString{
    NSMutableString *s = [NSMutableString stringWithString:aString];
    [s replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"/" withString:@"\\/" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\n" withString:@"\\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\b" withString:@"\\b" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\f" withString:@"\\f" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\r" withString:@"\\r" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\t" withString:@"\\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    return [NSString stringWithString:s];
}

The error on the server is - Request format is unrecognized for URL unexpectedly ending in '/stakeholders'. Which is an error in asp.net throws when deserializing the posted JSON

The output I am receiving on the client is

Request failed with error: Error Domain=com.alamofire.networking.error Code=-1011 "Expected status code [number of indexes: 100 (in 1 ranges), indexes: (200-299)], got 500" UserInfo=0x8e95e60 {NSErrorFailingURLKey=https://somedomain.com/api/stakeholders, NSLocalizedDescription=Expected status code [number of indexes: 100 (in 1 ranges), indexes: (200-299)], got 500}, { NSErrorFailingURLKey = "https://somedomain.com/api/stakeholders"; NSLocalizedDescription = "Expected status code [number of indexes: 100 (in 1 ranges), indexes: (200-299)], got 500"; } 2013-02-25 11:03:44.669 NetworkPlugin[60069:c07]

2
500 is an internal server error, check the server's error logs.Kevin
The error on the server is - Request format is unrecognized for URL unexpectedly ending in '/stakeholders'. Which is an error in asp.net throws when deserializing the posted JSONAshish Agarwal
That error doesn't sound like it has anything to do with the JSON. Are you sure you have a route set up for /stakeholders, and it's set to take POST requests?Kevin
yes. I am able to make the POST request to the same server from a website using JQueryAshish Agarwal
Check out AFHTTPClient's setParameterEncoding method with AFJSONParameterEncoding so the post will actually have a valid JSON HTTP body.Keith Smiley

2 Answers

3
votes

Check out AFHTTPClient's setParameterEncoding method with AFJSONParameterEncoding so the post will actually have a valid JSON HTTP body. Declared and commented here

1
votes

What I see is you are sending "," instead of values such as "15", "18" etc. Try modifying your json data using correct JSONKit functions.