I've been trying to do an HTTP post to my localhost using swift for a mobile app, it prints Result -> Optional(["user": larry]) which should mean it works but it isn't posting anything on my localhost. My code is:
func ReqUsers() -> Void {
let json = ["user":"larry"]
do {
let jsonData = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
let url = NSURL(string: "http://192.168.1.318:8080")! /*localhost*/
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in
if error != nil{
print("Error -> \(String(describing: error))")
return
}
do {
let result = try JSONSerialization.jsonObject(with: /*data!*/ request.httpBody!, options: .allowFragments) as? [String:AnyObject]
print("Result -> \(String(describing: result))")
} catch {
print(response!)
print("Error -> \(error)")
print("that")
}
}
task.resume()
} catch {
print(error)
print("this")
}
print("called")
}
thanks for your time
for more info on the backend it's being written with goLang and here is the code:
package main
import (
"fmt"
//"os"
//"io/ioutil"
//"log"
"net/http"
)
func main() {
http.HandleFunc("/", HelloServer)
http.ListenAndServe(":8080", nil)
}
func HelloServer(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello")
}