Search code examples
iosswiftcore-dataxcode7

Why URLs from NSUserDefaults and CoreData don't allow to send POST request?


I need help, 'cause I've run into following problem and don't know about any reasons of it.

If I declare my POST request like this:

    let urlString = url.valueForKey("url") as? String
    let myUrl = NSURL(string: urlString!);

    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";

    let postString = "imei=AA022F8E-9E20-482D-80CB-DE06A8CD0990&pin=1234";
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

or like this:

    requestURL = NSKeyedUnarchiver.unarchiveObjectWithData(savedPeople) as! String
    let myUrl = NSURL(string: requestURL!);

    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST"      

    let postString = "imei=AA022F8E-9E20-482D-80CB-DE06A8CD0990&pin=1234";
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

it will looks like I sending empty POST request.

But if I do it in common way, like this:

    let myUrl = NSURL(string: "http://www.wtek.ru/test.php");
    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";
    let postString = "imei=AA022F8E-9E20-482D-80CB-DE06A8CD0990&pin=1234";
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

it will work correctly.

Also, first two version work correctly when I send request to http://127.0.0.1/request.php, but it doesn't work when I try to send my request outside of local network.

Where can be the problem?


Solution

  • The problem was not in Core Data / NSUserDefaults.

    It happened because I've put to Core Data link without www. So, if you faced with same problem, please, double-check it.