1

By continuing from this question ,

I am trying to convert [String : Any] into String and then passing that String into forHTTPHeaderField

Attempt 1: Without Pretty

let encoder = JSONEncoder()

if let json = try? encoder.encode(jsonDict) {
   convertedString = String(data: json, encoding: .utf8)!
}

print("JsonStringFormat     ", convertedString )

let url = NSURL(string: getMenuURL)
let request = NSMutableURLRequest(url: url! as URL)
request.setValue(convertedString, forHTTPHeaderField: "SessionInfo")

print("\nHEADer__reQQ__    ", request.allHTTPHeaderFields)

OUTPUT:

JsonStringFormat      {"Token":"96FFC5B994514B3D","UICulture":"en-CA ","LanguageCode":"ENG","CompanyID":"QAP","IMEINo":"1jzBG3TSrMzj\/tKihlEv8g=="}
HEADer__reQQ__     ["SessionInfo": "{\"Token\":\"96FFC5B994514B3D\",\"LanguageCode\":\"ENG\",\"UICulture\":\"en-CA \",\"CompanyID\":\"QAP\",\"IMEINo\":\"1jzBG3TSrMzj\\/tKihlEv8g==\"}"]

Attempt 2: With .pretty printed

let encoder = JSONEncoder()

// ADDING PRETTY FORMAT
encoder.outputFormatting = .prettyPrinted

if let json = try? encoder.encode(jsonDict) {
   convertedString = String(data: json, encoding: .utf8)!
}

print("PrettyJsonStringFormat     ", convertedString )

let url = NSURL(string: getMenuURL)
let request = NSMutableURLRequest(url: url! as URL)
request.setValue(convertedString, forHTTPHeaderField: "SessionInfo")

print("\nPrettyHeader__    ", request.allHTTPHeaderFields)

OUTPUT:

PrettyJsonStringFormat      {
  "Token" : "70E277954143414A",
  "UICulture" : "en-CA ",
  "LanguageCode" : "ENG",
  "CompanyID" : "QAP",
  "IMEINo" : "1jzBG3TSrMzj\/tKihlEv8g=="
}

PrettyHeader__    [:]

If I go with Attempt 1, BackSlash \ is appending in that value. To avoid that I go with Attempt 2, [Pretty Printed] .

I don't know why request.allHTTPHeaderFields not having that added header values.

Kindly guide me.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
McDonal_11
  • 3,935
  • 6
  • 24
  • 55
  • Why `prettyPrinted`? The server doesn't care about legibility. – vadian Jun 14 '19 at 12:13
  • In my previous question, https://stackoverflow.com/questions/56582434/swift-how-to-add-dictionary-format-in-forhttpheaderfield-in-get-request in Postman screenshot, I have given value for **SessionInfo** key in **JSON** format. I have tried same in **forHTTPHeaderField** in Swift. It accepts only in String format. So , I have converted to String and then pass value in **forHTTPHeaderField**. I got error in response. Now I am trying to print **allHTTPHeaderFields** , printed string contains, "\" . I thought this "\" may be an issue. So I go for **prettyprinted** . – McDonal_11 Jun 17 '19 at 04:46

2 Answers2

1

You should Check this answer in this link

Your understanding of the standard is correct. In the past, multi-line header values were supported under RFC 2616. This feature was known as "Line Folding":

HTTP/1.1 header field values can be folded onto multiple lines if the continuation line begins with space or horizontal tab. All linear white space, including folding, has the same semantics as SP. A recipient MAY replace any linear white space with a single SP before interpreting the field value or forwarding the message downstream.

dimohamdy
  • 2,917
  • 30
  • 28
0

That's because convertedString in Attempt2 has multiple line.

RFC says header field value having multiple lines are deprecated.

Historically, HTTP header field values could be extended over multiple lines by preceding each extra line with at least one space or horizontal tab (obs-fold). This specification deprecates such line folding except within the message/http media type (Section 8.3.1). A sender MUST NOT generate a message that includes line folding (i.e., that has any field-value that contains a match to the obs-fold rule) unless the message is intended for packaging within the message/http media type.

And, setValue(_:forHTTPHeaderField:) seems to ignore such values.

// This does nothing. Just ignoring the value "A\nB"
request.setValue("A\nB", forHTTPHeaderField: "C")

In addition, backslash in Attempt1 will have no problem. The server that receives the request will handle the value properly.

Community
  • 1
  • 1
pompopo
  • 929
  • 5
  • 10
  • Thanks @pompopo , Can u help me on this? https://stackoverflow.com/questions/56582434/swift-how-to-add-dictionary-format-in-forhttpheaderfield-in-get-request – McDonal_11 Jun 14 '19 at 10:38
  • Is that question still not solved? You seem to have a valid String value already. – pompopo Jun 14 '19 at 11:14
  • If I am having valid Converted String, then I could able get some value in **data** , From previous question. – McDonal_11 Jun 14 '19 at 11:16
  • My Query: I am hitting **GET api**, with headers, and response as Image. **Postman** screenshot I have attached in previous question. But, via **Swift**, I couldn't achieve this. Can guide me?? – McDonal_11 Jun 14 '19 at 11:18
  • Problem Facing: In **Postman** , I can able to add header in **JSON format**. But, in **Swift** , I couldn't do this. So I am trying to convert, that **JSON format** to String. Though I converted to String, api result is **Error** – McDonal_11 Jun 14 '19 at 11:23
  • Is the api server under your control? If so, you can review the difference of the requests, postman and Swift. – pompopo Jun 14 '19 at 11:38
  • So sad.. v don't have that control. – McDonal_11 Jun 14 '19 at 11:42
  • What error are you getting now? Is it an api error? Show your current `allHTTPHeaderFields` . If it has valid values, there may be another problem. – pompopo Jun 14 '19 at 12:25