2017-12-21 35 views
1

나는 I'have 내가 변환 URLComponents를 사용하여 사전을 탈출을 시도했습니다 사전httpBody를 올바르게 만들려면 어떻게해야합니까?

let params = ["key":"val","key1":"val1"] 

있어

Content-Type: application/x-www-form-urlencoded 

과 POST 방식의 몸을 만들려고합니다. 그러나 HTTP 사양에서 이러한 이스케이프 메소드가 동일하지 않음을 발견하지 못했습니다.

누군가 올바른 해결책을 알고 있습니까?

나는 당신과 NSURLComponents으로 몸을 만들어야 할 수

https://tools.ietf.org/html/draft-hoehrmann-urlencoded-01

https://tools.ietf.org/html/rfc1866

https://tools.ietf.org/html/rfc1738

https://tools.ietf.org/html/rfc3986

+0

본문에 인코딩 된 URL이 필요합니까? –

+0

더 구체적으로 무엇을하고 싶습니까 –

+0

@ReinierMelian 예. URLComponents.query가 올바른 구현인지 확실하지 않습니다. – Vyacheslav

답변

1

을 검토 한 결과 :

let components = NSURLComponents() 

components.queryItems = [ 
    URLQueryItem(name: "key", value: "val"), 
    URLQueryItem(name: "key1", value: "val1") 
] 
if let query = components.query { 
    let request = NSMutableURLRequest() 

    request.url = ... 
    request.allHTTPHeaderFields = [ "Content-Type": "application/x-www-form-urlencoded"] 
    request.httpBody = query.data(using: .utf8) 
} 

NSURLComponents은 유효한 URL을 데이터에서 생성하고 유효한 URL을 해당 구성 요소로 구문 분석합니다. 위의 내용 유형을 사용하는 HTTP 게시물 요청의 본문에는 URL 쿼리와 같은 매개 변수가 포함되어야합니다 (How are parameters sent in an HTTP POST request? 참조).

NSURLComponents이 표준에 부합하기 때문에 좋은 선택입니다.

도 참조하십시오 : WikipediaW3C.

+0

나는 똑같이 말했습니다. 나는 이것이 올바른 구현인지 확신 할 수 없다. – Vyacheslav

+0

@clement 나는 똑같이했다. – Vyacheslav

+0

stackoverflow url은 사양이 아닙니다 – Vyacheslav