2017-09-19 12 views
0

내 앱에서 API 호출을하는 동안 헤더에 위치를 전달해야합니다. 그러나 열쇠는 CamelCase로 변환됩니다.NSMutableURLRequest forHTTPHeaderField 키가 올바르게 설정되지 않았습니다.

let session = URLSession.shared 
let request = NSMutableURLRequest(url: url!) 
request.httpMethod = "GET" 

request.addValue("XYZLocation", forHTTPHeaderField: "location") 

let task = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in 

    if (error == nil){ 
     let str = String(data: data!, encoding: String.Encoding.utf8) 
     print("Response : \(String(describing: str))") 
    } 
    else{ 
     print("Error : \(String(describing: error?.localizedDescription))") 
    } 
}) 
print("Header : \(String(describing: request.allHTTPHeaderFields))") 
task.resume() 

로그 출력 내가 헤더 키 케이스 감도를 유지할 수있는 방법

Header : Optional(["Location": "XYZLocation"]) 

아래에 아래에 내 코드를 찾아주세요. 도와주세요.

+0

먼저 Swift와 동등한 'NSMutableURLRequest' 대신'URLRequest'를 사용하십시오. 둘째로,'addValue'의 [documentation] (https://developer.apple.com/documentation/foundation/nsmutableurlrequest/1407676-addvalue)를 보면 헤더 값이 대소 문자를 구분하지 않음을 알 수 있습니다 HTTP RFC로 변환하므로 백엔드에서 대소 문자를 구분하지 않는 헤더도 사용해야합니다. –

+0

@DávidPásztor 역시 URLRequest와 동일합니다. 또한 우리는 백엔드를 변경할 수 없습니다. 왜'Location' 헤더 만있는 겁니까? 다른 헤더 키에서 제대로 작동합니다. –

답변