The image is not transferred in full via POST to the server

I send the text to the server with picture via POST. The text come correctly, but the picture is not complete. 10 percent of the picture is displayed correctly, and other is just as gray background. Swift converts the image file to text using base64EncodedString().

Создаётся впечатление, что либо Swift с ошибкой производит преобразование, либо сервер не до конца принимает данные. Но я увеличит лимит для POST и всё равно не помогло. Так же менял значения сжатия картинки через compressionQuality, тоже не помогло.

It seems that Swift performs the conversion with an error, or the server does not fully receive the data. But I have increase the limit for POST and it doesn't help. I also changed the image compression values with compressionQuality, it did not help.

let uiImage: UIImage = self.selectedImage.asUIImage()
let imageData: Data = uiImage.jpegData(compressionQuality: 0.9) ?? Data()
let imageStr: String = imageData.base64EncodedString()

How to fix it?


POST request code:

func RequestPost(completion: @escaping((String) -> Void)) {
    let url = URL(string: self.newsAPI)
    guard let requestUrl = url else { fatalError() }
    var request = URLRequest(url: requestUrl)
    request.httpMethod = "POST"
    let postString = "message=\(self.newsMessage)&user=\(self.newsUser)&email=\(self.newsEmail)&media=\(self.newsMedia)"
    request.httpBody = String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
        if error != nil {
        if let data = data, let dataString = String(data: data, encoding: .utf8) {
            DispatchQueue.main.async {
                self.dataString = dataString

Read more here:

Content Attribution

This content was originally published by Lexxy at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: