user4246653
user4246653

Reputation:

JSON values POST in iOS?

How do I POST JSON values using a RESTFUL API?

Here I have added a sample API:

http://our.api.com/Search?term=pumas&filters={"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?

Upvotes: 0

Views: 70

Answers (1)

Mehul
Mehul

Reputation: 3178

Try the below code...

For that use the AFNetworking library. Link: https://github.com/AFNetworking/AFNetworking

#import "AFNetworking.h"

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

//Pass Perameters
parameters = @{@"dCurrentLat" : ApplicationDelegate.strLattitudeG,
               @"dCurrentLong":ApplicationDelegate.strLongitudeG,
               @"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];

    if(success)
    {
    }
}
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

// JSON
NSMutableData   *responseData;
NSURLConnection * Connection_Audio_Upload;

Set in .m File

NSMutableURLRequest *request = [NSMutableURLRequest
                                requestWithURL:[NSURL URLWithString:@"http://180.179.227.99/oMomento/webservice.asmx/UploadFileJSon"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                timeoutInterval:10];

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");

    }
}

Upvotes: 2

Related Questions