2

I want to pass a JSON dictionary as a parameter by using uploadUrl, but it gives me an Unsupported Url error with code-1002.

When I hit this URL on Postman, it works perfectly. How do I implement this using a JSON model?

NSString *uploadUrl =@"<Your host URL>";

[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:nil
                               completion:^(NSDictionary *json, JSONModelError *err)
{
    if(err == nil)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"success" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        completionHanldler(json);
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Failed" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];

        NSMutableDictionary *errorDict=[[NSMutableDictionary alloc]init];
        if(err.code==-1009)
            [errorDict setObject:@"The Internet connection appears to be offline."forKey:@"error"];
        else
            [errorDict setObject:@"Error occurred. Please try again!"forKey:@"error"];

        completionHanldler(errorDict);
    }
}];
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
  • I am not aware about the JSONHTTPClient but i think for pass json in url first you need to convert json in to the string and then pass then string as a argument. Thats why it is run in postman successfully – Chirag Shah Sep 03 '15 at 05:15
  • NSError * err; NSData * jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&err]; NSString *myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; Yes, I am converting it to string. But it doesn't work for me – Sourabh Sharma Sep 03 '15 at 05:17
  • @SourabhSharma never expose your domain & param details. – Dipen Panchasara Sep 03 '15 at 07:05

3 Answers3

1

stringByAddingPercentEscapesUsingEncoding

This method solved this issue. Previously I was assigning the unsupported URL.

NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&err];
NSString *myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// This line is the answer.
myString = [myString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

NSString *uploadUrl = [NSString stringWithFormat:@"<MY host URL>"?data=%@",myString];

[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:nil
                               completion:^(NSDictionary *json, JSONModelError *err)
{
    if(err == nil)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"success" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        completionHanldler(json);
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Failed" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];

        NSMutableDictionary *errorDict=[[NSMutableDictionary alloc]init];
        if(err.code==-1009)
            [errorDict setObject:@"The Internet connection appears to be offline."forKey:@"error"];
        else
            [errorDict setObject:@"Error occurred. Please try again!"forKey:@"error"];

        completionHanldler(errorDict);
    }
}];
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
0

Try this:

NSString *uploadUrl =@"<Your host URL>";
NSDictionary *val = @{<Add your params as key : value here>};
NSArray *innerParameter = @[val];
NSDictionary *parameter =@{@"Passports":innerParameter};


[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:parameter
                               completion:^(NSDictionary *json, JSONModelError *err)
{
    if(err == nil)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"success" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        NSError *error;
        NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:objectData  options:NSJSONReadingMutableContainers error:&jsonError]; // this is a dictionary NOT the JSON
        completionHanldler(resultDict);
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Failed" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];

        NSMutableDictionary *errorDict=[[NSMutableDictionary alloc]init];
        if(err.code==-1009)
            [errorDict setObject:@"The Internet connection appears to be offline."forKey:@"error"];
        else
            [errorDict setObject:@"Error occurred. Please try again!"forKey:@"error"];

        completionHanldler(errorDict);
    }
}];

EDIT:

Same network call using AFNetworking

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *val = @{<Params as key : value>};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"<your host URL>" parameters:val success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

EDIT 2:

First of all, I figured the backend is done in POST but it is going through URL encoding. After much trial and error, this CANNOT be done, even after trying it in GET. As far as I can understand, the backend is not recognizing the POST method in JSON format, so you have to send it in URL encoding fashion only.

Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
  • Have you run this code?? it showing me expected expression error in innerParameter dictionary. – Sourabh Sharma Sep 03 '15 at 05:29
  • Error Domain=JSONModelErrorDomain Code=2 "Bad network response. Probably the JSON URL is unreachable." UserInfo=0x14f0c180 {NSLocalizedDescription=Bad network response. Probably the JSON URL is unreachable.} This error is occured?? please help. – Sourabh Sharma Sep 03 '15 at 05:45
  • just deleted the "?"mark try once, i cannot reach this but try once and confirm plz – Saheb Roy Sep 03 '15 at 05:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88652/discussion-between-sourabh-sharma-and-saheb-roy). – Sourabh Sharma Sep 03 '15 at 05:56
0

Reviewing your question and API. you want to send some information to backend, you need to POST information using POST method and your data as json format.

you can test request using any online API Proxies (eg. POSTMAN or hurlit).

I hope its clear to you, you can't send more than 256 bytes using GET request, and as your API is POST request. In your existing method make following changes and you are done.

NSString *uploadUrl =@"<Your host URL>";
NSDictionary *dictParams = @{@"key" : @"value"};
[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:dictParams
                               completion:^(NSDictionary *json, JSONModelError *err)
 {
 }];
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57