Swift http Post说它可以工作,但它不是';t在本地主机上发布

Swift http Post说它可以工作,但它不是';t在本地主机上发布,swift,http,go,post,Swift,Http,Go,Post,我一直在尝试使用swift为移动应用程序向本地主机发送HTTP post,它打印Result->Optional([“user”:larry]),这意味着它可以工作,但没有在本地主机上发布任何内容。我的代码是: func ReqUsers() -> Void { let json = ["user":"larry"] do { let jsonData = try JSONSerializ

我一直在尝试使用swift为移动应用程序向本地主机发送HTTP post,它打印
Result->Optional([“user”:larry])
,这意味着它可以工作,但没有在本地主机上发布任何内容。我的代码是:

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")
    }
谢谢你的时间

有关后端的更多信息,正在使用goLang编写,以下是代码:

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")
}

如果在服务器上看不到日志,可能是由于
URLSession
的缓存策略造成的

尝试将
URLRequest
cachePolicy
属性设置为或,然后查看是否有效:

let url = URL(string: "http://192.168.1.318:8080")! /*localhost*/
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
// or
request.cachePolicy = .reloadIgnoringCacheData

您应该将
数据
传递到
JSONSerialization.jsonObject(带:)
。Not
request.httpBody
@RobertCrabtree我最初尝试过,但这样做会产生一个错误:Error->Error Domain=nscoaerrordomain code=3840“字符0周围的无效值”。UserInfo={NSDebugDescription=字符0周围的无效值。}`您需要找出
数据中存储的内容<代码>打印(字符串(数据:data!,编码:.utf8)?“无效”)
@RobertCrabtree它提供由服务器打印的数据“Hello”@camilebasbous所以,如果打印服务器响应,会出现什么问题?感谢您花时间处理此问题,但事实并非如此。使用“我的服务器”返回:缺少协议方案退出状态1这是服务器端问题:检查服务器是否正常,它可以获取并发布您看到的是主机内带有/for all路径的侦听端口,因为get已写入完整URL,但它无法识别移动应用程序发送的任何内容(如果需要)。我们可以视频聊天,这样我可以告诉你发生了什么on@CamilleBasbous你有没有API调用的工作示例,比如说邮递员?