2

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:

enter image description here

Postman Screenshot

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

enter image description here

How to solve this issue?

James Z
  • 12,209
  • 10
  • 24
  • 44
McDonal_11
  • 3,935
  • 6
  • 24
  • 55
  • `print("\n\nCAtcLL___Err ",error.localizedDescription)` That's the line that prints the output? What's `WebService.sharedManager().mobInfoDict`? – Larme Jun 13 '19 at 14:16
  • yes, this line prints output. This is, WebService.sharedManager().mobInfoDict , [String:Any], {"CompnayID" : "Qa55", "Token" : "123", "number" : "916"} – McDonal_11 Jun 13 '19 at 14:17
  • What's the content of `WebService.sharedManager().mobInfoDict`? It's not JSON valid apparently. Once you made if JSON valid (ie, only String, Number, Null, Dict & Array). – Larme Jun 13 '19 at 14:18
  • WebService.sharedManager().mobInfoDict = {"CompnayID" : "Qa55", "Token" : 123, "number" : 916, "Language" : "en-CA"} – McDonal_11 Jun 13 '19 at 14:20
  • The same JSON format, I have given in POSTMAN, that's working fine. – McDonal_11 Jun 13 '19 at 14:21
  • Is that a String? Because that's not a valid "Swift Dict declaration"? Is that a String? That's unclear, that's not valid Swift Code. How did you set it? Could you print it? – Larme Jun 13 '19 at 14:22
  • I will give u, but only one doubt is thr, How to add Dictionary in forHTTPHeaderField ? – McDonal_11 Jun 13 '19 at 14:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194891/discussion-between-mcdonal-11-and-larme). – McDonal_11 Jun 13 '19 at 14:24
  • By converting it into a JSON String apparently. But your error says that's it's can't be transformed into JSON. So there is an issue there. – Larme Jun 13 '19 at 14:24
  • @Larme , Hope, this may be an issue?? https://stackoverflow.com/questions/56593307/swift-unable-to-add-prettyprinted-format-string-to-forhttpheaderfield – McDonal_11 Jun 14 '19 at 07:20

0 Answers0