-1

I'm using AFNetworking and find no way to get correct response, searched ways but closely able to get the below code but still getting error

Also i have another issue. Is there any way to find correct issue in web services? So far i am finding it very difficult to troubleshoot the mistake.

because server always return error code, i need some debugging tool which can able to tell like you have not placed header or incorrect content type, invalid format....

It just returns error code and that means a lot more....

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.somewebsiteurl/webservice/getResponse"]];
[request setHTTPMethod:@"POST"];

//set headers
NSString *contentType = [NSString stringWithFormat:@"text/html"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];


//create the body
NSMutableData *postBody = [NSMutableData data];

[postBody appendData:[[NSString stringWithFormat:@"data={\"request\":{\"type\":\"getwork\"}}"] dataUsingEncoding:NSUTF8StringEncoding]];


//post
[request setHTTPBody:postBody];

//Response

{"response":{"message":"Invalid Request"}}

when i place this in http://apikitchen.com/ my webservice works

Ankuribc
  • 59
  • 1
  • 5

2 Answers2

0

please visit AFNetworking github page github link to know about AFNetworking and here is the code given to send request using post in AFNetworking

Edit:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"request":@{@"type":@"getwork"}:@"data"};
[manager POST:@"http://www.somewebsiteurl/webservice/getResponse" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

replace your parameters and send request

Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
0

There are a few issues in how you setup the request.

But before anybody can answer this question, you need to specify what the server expects as a valid POST data. In your example, you are trying to pass a string:

data={"request":{"type":"getwork"}}

This string cannot be encoded to a parameter list required for an application/x-www-form-urlencoded MIME type. That, is AFNetworking - out of the box - cannot be used with this parameter string.

It's also not JSON. That is, AFNetworking with content type set to application/json can also not be used.

So, first define the syntax and the MIME type the server can accept.

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67