Question

I'm running into some difficulties making requests to a backend that is being worked on for me. For example, a user I've previously created with the username: "test23" and it's password: "ws0ei" works perfectly when making this JSON request like this:

self.username = @"test23";
self.password = @"ws0ei";

NSDictionary *jsonDictionary = @{@"action" : @"upload_profile_image",
                                    @"username" : self.username,
                                    @"password_hash" : [self md5HexDigest:self.password]};

NSString *requestString = [[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:jsonDictionary options:kNilOptions error:nil] encoding:NSUTF8StringEncoding];

NSString *escaped = [[NSString stringWithFormat:@"http://www.host.com/server.php?request=%@",requestString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:escaped]];

- (NSString*)md5HexDigest:(NSString*)input
{
    const char* str = [input UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, strlen(str), result);

    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];

    for (int i = 0; i<CC_MD5_DIGEST_LENGTH; i++)
    {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}

However, when testing the same request with the username that I created "test32" and its same password of "ws0ei", the API responds with

{
    reason = "Invalid username/password";
    status = "image not uploaded";
}

I have no idea what the problem is, I'm thinking it's related to improper encoding or something related to the md5 on the app side and not the backend definitely.

I'd appreciate any help offered. Thank you. Edit: including some console logs of how user "test23" JSON dictionary, request URL, and request response looks like, and also user "test32" to show you that they're identical"

Username: test23

JSON Dictionary

2014-05-01 20:53:51.311 test[20603:60b] 
{
    action = "upload_profile_image";
    "password_hash" = 19144b5589ca801f459fe42db9ec859a;
    username = test23;
}

Request URL (escaped)

2014-05-01 20:53:51.312 test[20603:60b] http://www.host.com/server.php?request=%7B%22username%22:%22test23%22,%22action%22:%22upload_profile_image%22,%22password_hash%22:%2219144b5589ca801f459fe42db9ec859a%22%7D

Response

2014-05-01 20:53:51.850 test[20603:60b] 
{
    status = "image uploaded";
}

Username: test32

JSON Dictionary

2014-05-01 20:57:00.988 test[20625:60b] 
{
    action = "upload_profile_image";
    "password_hash" = 19144b5589ca801f459fe42db9ec859a;
    username = test32;
}

Request URL (escaped)

2014-05-01 20:57:00.989 test[20625:60b] http://www.host.com/server.php?request=%7B%22username%22:%22test32%22,%22action%22:%22upload_profile_image%22,%22password_hash%22:%2219144b5589ca801f459fe42db9ec859a%22%7D

Response

2014-05-01 20:57:01.464 test[20625:60b] 
{
    reason = "Invalid username/password";
    status = "image not uploaded";
}
Was it helpful?

Solution

Turns out this was indeed a backend issue. Problem has been solved now

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top