Swift:如何在GET请求中的forHTTPHeaderField中添加Dictionary格式

时间:2019-06-13 14:11:04

标签: swift nsurlsession nsurlrequest nsmutableurlrequest

在GET请求中,我正在尝试添加标题。

request.setValue(value: String?, forHTTPHeaderField: "SessionInfo")

但是,SessionInfo的值在[String : Any]

尝试1:

func SO_Function() {

    let service_url = WebService.sharedManager().serviceURL
    let getMenuURL = service_url + "/MyPage/FileDownload.ashx"

    var convertedString = ""

    do {
        let data1 = try JSONSerialization.data(withJSONObject: WebService.sharedManager().mobInfoDict, options: [])
        convertedString = String(data: data1, encoding: .utf8)!
        print("\n\nconvertedJSONtoData  ", convertedString)

    } catch {
        print("\n\nCAtcLL___Err   ",error.localizedDescription)
    }

    let url = NSURL(string: getMenuURL)
    let request = NSMutableURLRequest(url: url! as URL)

    //Below line, If I can able to add [String:Any] then I will get 
    proper Image as output.

    request.setValue(convertedString, forHTTPHeaderField: "SessionInfo")
    request.setValue("67a2a6fb1d13450a", forHTTPHeaderField: "Flowid")
    request.setValue("d29566ac42de4e99", forHTTPHeaderField: "Fileid")
    request.setValue("LMS_LEAVEREQUEST", forHTTPHeaderField: "Form")

    request.httpMethod = "GET"

    let session = URLSession.shared

    let mData = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
        if let res = response as? HTTPURLResponse {

            DispatchQueue.main.async {

                let img = UIImage(data: data!)
                self.attachmentImgVw.image = img
            }

        }else{
            print("\n\nError: \(String(describing: error))")
        }
    }
    mData.resume()
}

输出

  

由于数据格式不正确,因此无法读取。

错误Scre​​enShot 1:

enter image description here

邮递员屏幕截图

在邮递员中,我以SessionInfo格式提供Dictionary值。工作正常。

enter image description here

如何解决此问题?

0 个答案:

没有答案