Imran Imran - 1 year ago 89
iOS Question

JSON values POST in iOS?

How do I POST JSON values using a RESTFUL API?

Here I have added a sample API:{"productType":["Clothing","Bags"],"color":["Black","Red"]}

I want to post values like the above URL and getting a response. Is there any possible way to get values? Does it have a sample API?

Answer Source

Try the below code...

For that use the AFNetworking library. Link:

#import "AFNetworking.h"

string = [NSString stringWithFormat:@"%@offer/nearBy", WSURL];
NSDictionary *parameters;

//Pass Perameters
parameters = @{@"dCurrentLat" : ApplicationDelegate.strLattitudeG,
               @"start":[NSString stringWithFormat:@"%d",start],
               @"limit":[NSString stringWithFormat:@"%d",20],
               @"iCountryID" : strCountyID};

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

//Set header here
[manager.requestSerializer setValue:XAPIKEY forHTTPHeaderField:@"X-API-KEY"];

[manager POST:string parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
    //Here you can Get Response from Server
    NSDictionary *aDictResponse = (NSDictionary *)responseObject;
    BOOL success = [[aDictResponse objectForKey:@"SUCCESS"] boolValue];

failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    //Here error log

Another way...

//Use below the code to request the POST method. It is simple

Set in .h File

NSMutableData   *responseData;
NSURLConnection * Connection_Audio_Upload;

Set in .m File

NSMutableURLRequest *request = [NSMutableURLRequest
                                requestWithURL:[NSURL URLWithString:@""] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData

NSString * params=[NSString stringWithFormat:@"f=%@&step=0&filename=%@.3gp&path=%@",trimmed,audio_File_Name,@"BabyJournal/Audio/"];
[request setHTTPMethod:@"POST"];

[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
Connection_Audio_Upload=[[NSURLConnection alloc] initWithRequest:request delegate:self];

//-----Using--------JSON methods------------//

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    if (connection == Connection_Audio_Upload)
        [responseData setLength:0];

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    if (connection == Connection_Audio_Upload)
        [responseData appendData:data];

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    if (connection == Connection_Audio_Upload)
        NSLog(@"%@", error);

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
    if (connection == Connection_Audio_Upload)
        // NSLog(@"Finished Loading Pic Upload............");

        //You can get the response HERE//
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        // NSLog(@"respoce string = %@",responseString);
        NSLog(@"Succesfully Upload Pic Audio_Upload");

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download