Swift: How to add Dictionary format in forHTTPHeaderField in GET request

拟墨画扇 提交于 2019-12-11 15:59:11

问题


In GET request, I am trying to add header.

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

But, SessionInfo value is in [String : Any]

Attempt 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()
}

Output

The data couldn’t be read because it isn’t in the correct format.

Error ScreenShot 1:

Postman Screenshot

In postman, I am giving SessionInfo value as in Dictionary format. Working fine.

How to solve this issue?

来源:https://stackoverflow.com/questions/56582434/swift-how-to-add-dictionary-format-in-forhttpheaderfield-in-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!