简体   繁体   中英

How to pass JSON request

I am new to AFNetworking framework. I try to send JSON request to the server to get JSON response, so i tried something like this:

NSURL *url = [NSURL URLWithString:@"http://data.mycity.gov/api/views/INLINE/rows.json?method=index"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setValue:[NSString stringWithFormat:@"application/json"] forHTTPHeaderField:@"Content-Type"];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSLog(@"Success");
    } failure:^(NSURLRequest* req,NSHTTPURLResponse *req2, NSError *error,id mex) {
        NSLog(@"Failed");
        NSLog(@"%@",[error description]);

    }];

    [operation start];  

So i always find my self into the failure block, with this error description:

Error Domain=com.alamofire.networking.error Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0x7b58030 {NSErrorFailingURLKey=http://data.mycity.gov/api/views/INLINE/rows.json?method=index, NSLocalizedDescription=Expected status code in (200-299), got 400}

Any ideas? am i missing something?

Status Code of 400 means - Bad Request The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

When I try to go to that URL: http://data.mycity.gov/api/views/INLINE/rows.json?method=index " , I get a not found error

Make sure the API you are using is correct, if the web service returns JSON, you should be able to type that URL into your browser and get a JSON response.

It seems your server is returning 400, "Bad Request". Check that your URL is correct.

BTW your stringWithFormat is redundant. You can replace

[NSString stringWithFormat:@"application/json"] 

with

@"application/json"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM