I need to send an array of bytes to the server and receive another array of bytes in response, but the response comes
Status Code: 400, Headers {
"Content-Type" = (
"text / html; charset = UTF-8"
);
}
my code
let byteArray: [UInt8] = [UInt8](arrayLiteral: 0x05, 0x00, 0x07, 0x04, 0x00)
let arrData = Data(byteArray)
let requestURL = URL(string: "http://192.168.4.1/")!
var request = URLRequest(url: requestURL)
request.httpMethod = "POST"
let session = URLSession(configuration: URLSessionConfiguration.default)
request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
request.addValue(String(arrData.count), forHTTPHeaderField: "Content-Length")
request.httpBody = arrData
var task = session.dataTask(with: request){ data, response, error in
print("DATA (data ?? Data()) RESPONSE (response) ERROR (error)")
}
task.resume()
question from:
https://stackoverflow.com/questions/65888948/ios-swift-post-%d1%80%d0%b5%d0%b5%d0%b7-request-with-binary-body 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…