0

Using Postman on my Mac I have confirmed GET/POST works to my endpoint. On my iPad I am trying to do the same thing but only GET connects and returns data (just for testing).

In Postman I have key of devices and value of [{"name":"1","values":[121,182,243]}] From Postman I can Send and the physical object responds and the server returns an array of all devices and nothing else (which I do not require but that is how it goes). Postman does have x-www-form-urlencoded under Body. This works as expected from Postman.

From my iPad the following code always returns an error 400 which I think means I am providing something the server is not expecting.

+(void)makeRequest {

NSError *error = nil;

storedURL = [[NSUserDefaults standardUserDefaults] objectForKey:@"eb-ipaddress-saved"];
NSString *urlstring = [NSString stringWithFormat:@"http://%@",storedURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlstring] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSString *jsonPostBody = @"{\"devices\":[{\"name\":\"1\",\"values\":[121,182,243]}]}";
NSData *postData = [jsonPostBody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    if(httpResponse.statusCode == 200)
    {
        NSError *parseError = nil;
        NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
        NSLog(@"The response is - %@",responseDictionary);
        NSInteger success = [[responseDictionary objectForKey:@"success"] integerValue];
        if(success == 1)
        {
            NSLog(@"SUCCESS");
        }
        else
        {
            NSLog(@"FAILURE");
        }
    }
    else
    {
        NSLog(@"Error");
    }
}];
[dataTask resume];

}

My 4 logs from above (removed for clarity) return: urlstring http://192.168.90.55:3000/dmx/set jsonPostBody {"devices":[{"name":"1","values":[121,182,243]}]} httpResponse 400 Error

I will eventually switch to variables in my POST when in production.

Thank you

malaki1974
  • 1,605
  • 3
  • 16
  • 32
  • 1
    `[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];`: You are saying that you'll send the parameters as URLEncoded, but you send them as JSON... I guess that's the issue. it's like saying: I'll send you the letter in French, and you send actually Chinese... Note that Postman can generate Objective-C code that could help you find the differences. – Larme Apr 29 '22 at 22:45
  • I tried the first thought you had and still no dice. BUT - your closing comment was the answer. The code generated by Postman (didn't know it could do that) worked after one small correction of adding http://. – malaki1974 Apr 30 '22 at 11:58

1 Answers1

0

Thanks to Larme's answer I used the code from Postman and that worked.

+(void)makeRequest 
{

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.90.55:3000/dmx/set"]
  cachePolicy:NSURLRequestUseProtocolCachePolicy
  timeoutInterval:10.0];
NSDictionary *headers = @{
  @"Content-Type": @"application/x-www-form-urlencoded"
};

[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"devices=[{\"name\":\"1\",\"values\":[121,182,243]}]" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];

[request setHTTPMethod:@"POST"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  if (error) {
    NSLog(@"%@", error);
    dispatch_semaphore_signal(sema);
  } else {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSError *parseError = nil;
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"%@",responseDictionary);
    dispatch_semaphore_signal(sema);
  }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
malaki1974
  • 1,605
  • 3
  • 16
  • 32
  • Remove the semaphores, and just replace `NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"devices=[{\"name\":\"1\",\"values\":[121,182,243]}]" dataUsingEncoding:NSUTF8StringEncoding]]; ` in your previous code, it's better. – Larme Apr 30 '22 at 17:01