-1

I'm able to load this URL in a safari browser and see the JSON data I need, but this STHTTPRequest is returning null, making me not able to use the JSON, what is going wrong here? I am loading in safari

let url = "192.168.101.1:8080/api"
let request = STHTTPRequest.buildGET(URL(string: url), timeout: 15.0)!

2 Answers2

1

First of all check you are using the latest STHTTPRequest from CocoaPods STHTTPRequest.

Then you can simply use the following to initialise and call a request to the url. The result is obtained through body in completionBlock

guard let request = STHTTPRequest(urlString: url) else {return}
request.timeoutSeconds = TimeInterval(floatLiteral: 15.0)
request.completionBlock = { (headers, body) in
           print(body)
}
request.errorBlock = { (error) in
        // ...
}
request.startAsynchronous()
dbaekajnr
  • 11
  • 3
  • it's up to date, even with that implementation it returns null – DisplayName Jul 12 '19 at 19:21
  • 1. okay make sure to add http:// prefix to your url. 2. Also make sure App Transport Security settings is disabled in info.plist or add your domain as an exception. 3. If you are hosting locally with php without the Rewrite rules, do add .php to your endpoint. – dbaekajnr Jul 12 '19 at 23:58
0

needed to add http:// to the beginning of the url, giving http://192.168.101.1:8080/api/status