【发布时间】:2019-09-03 00:31:15
【问题描述】:
curl请求示例如下,我在swift中无法将其转换为url请求。
curl --include --request POST \
--header "application/x-www-form-urlencoded" \
--data-binary "grant_type=client_credentials&client_id=PUBLIC_KEY&client_secret=PRIVATE_KEY" \
'https://api.tcgplayer.com/token'
这是我认为它应该看起来的样子,但它没有按我的预期工作。
guard let url = URL(string: "https://api.tcgplayer.com/token") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("grant_type=client_credentials&client_id=\(TCGPlayerClient.publicKey)&client_secret=\(TCGPlayerClient.privateKey)", forHTTPHeaderField: "data-binary")
【问题讨论】:
-
你需要设置
httpBody数据,而不是作为新的header发送。