发送 POST 请求后控制台中的消息

Posted

技术标签:

【中文标题】发送 POST 请求后控制台中的消息【英文标题】:Message in console after sending POST request 【发布时间】:2021-03-29 11:00:31 【问题描述】:

我是 Swift 和 SwiftUI 的新手。单击按钮时,我需要向服务器发送POST 请求。请求发送,一切正常。

ShareHelper.swift:

import Foundation

class ShareHelper 

    var dataString: String = ""
    var newsMessage: String
    var newsUser: String
    var newsEmail: String
    var newsMedia: String
    let newsAPI: String = "https://example.com/api/shareNews"

    init(message: String, user: String, email: String, media: String) 
        self.newsMessage = message
        self.newsUser = user
        self.newsEmail = email
        self.newsMedia = media
        self.RequestPost()
    

    func RequestPost() 
        let url = URL(string: self.newsAPI)
        guard let requestUrl = url else  fatalError() 
        var request = URLRequest(url: requestUrl)
        request.httpMethod = "POST"
        let postString = "message=\(self.newsMessage)&user=\(self.newsUser)&email=\(self.newsEmail)&media=\(self.newsMedia)"
        request.httpBody = postString.data(using: String.Encoding.utf8)
        let task = URLSession.shared.dataTask(with: request)  (data, response, error) in
            if error != nil 
                return
            
            if let data = data, let dataString = String(data: data, encoding: .utf8) 
                self.dataString = dataString
            
        
        task.resume()
    


来自ContentView.swift的按钮

Button(action: 
    let shareHelper = ShareHelper(message: self.textAreaText, user: self.inputUser, email: self.inputEmail, media: self.inputMedia)
    shareHelper.RequestPost()
    print("Share helper: \(shareHelper.dataString)")
, label: 
    Text("Send")
)

如果POST请求发送成功,如何让消息出现在控制台?

【问题讨论】:

您可能会在这里找到答案? ***.com/questions/26364914/… RequestPost添加completionhandler? 【参考方案1】:

你需要使用完成块。

func RequestPost(completion: @escaping((String) -> Void)) 
    let url = URL(string: self.newsAPI)
    guard let requestUrl = url else  fatalError() 
    var request = URLRequest(url: requestUrl)
    request.httpMethod = "POST"
    let postString = "message=\(self.newsMessage)&user=\(self.newsUser)&email=\(self.newsEmail)&media=\(self.newsMedia)"
    request.httpBody = postString.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with: request)  (data, response, error) in
        if error != nil 
            return
        
        if let data = data, let dataString = String(data: data, encoding: .utf8) 
            DispatchQueue.main.async 
                self.dataString = dataString
                completion(dataString)
            
        
    
    task.resume()

在按钮上

Button(action: 
    let shareHelper = ShareHelper(message: "self.textAreaText", user: "self.inputUser", email: "self.inputEmail", media: "self.inputMedia")
    shareHelper.RequestPost  (dataString) in
        print("Share helper: \(shareHelper.dataString)")
        // Or
        print("Share helper: \(dataString)")
    
    
, label: 
    Text("Send")
)

【讨论】:

以上是关于发送 POST 请求后控制台中的消息的主要内容,如果未能解决你的问题,请参考以下文章

POST 请求成功后打开第二个控制器

java 实现长连接接受信息,发送信息

将多个对象从Angular控制器POST到Web API 2

Laravel:使用 POST 请求将数据发送到控制器方法

无法使用javascript向ASP.NET web api Controller发送http POST请求

vue跨域请求时报403