在POST请求上获取错误400

时间:2018-03-26 15:48:51

标签: android python http kotlin

因此,我有一个简单的Python服务器,它使用简单的文本和应该发送请求的Android应用程序来监听POST请求。我一直有代码400:Bad Request。 这是我的代码:

fun post (url: URL, data: String) : String
{
    val connection = url.openConnection() as HttpsURLConnection

    connection.doOutput = true
    connection.doInput = true
    connection.connectTimeout = 5000
    connection.readTimeout = 5000
    connection.requestMethod = "POST"

    connection.setRequestProperty("ImageBase64", data)        

    connection.connect()

    val rcode = connection.responseCode
    //val response = connection.responseMessage

    return rcode.toString()
}

非常感谢任何帮助。

UPD:我发送一个Base64编码的图片作为字符串。

这是我的服务器代码:

import base64
from flask import Flask, request

app = Flask(__name__)


@app.route('/', methods=["POST"])
def imageget():
    image_base64 = request.form['ImageBase64']

    with open("C:/CatImage.png", "wb") as Image:
        Image.write(base64.decodebytes(bytes(image_base64, 'utf-8')))
    return 'Success!'


if __name__ == '__main__':
    app.run()

0 个答案:

没有答案