最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】REST工具支持POST发送Transfer-Encoding chunked的二进制数据

Server crifan 1025浏览 0评论

折腾:

【已解决】Flask的REST API添加支持POST时body中分块传输二进制数据

期间,需要去测试REST的POST中发送

Transfer-Encoding: chunked

的二进制数据

先去看看之前强大的Postman:

postman post Transfer-Encoding chunked

Feature Request – Support for Transfer-Encoding: chunked · Issue #3684 · postmanlabs/postman-app-support

好像不支持?

HTTP Parse Error when POST request has `Transfer-Encoding: chunked` · Issue #1492 · puma/puma

Header “Transfer-Encoding: chunked” set even if Content-Length is provided which causes body to not actually get chunked · Issue #1648 · requests/requests

IBM Knowledge Center – Using chunked transfer-coding to send an HTTP request or response

spring – HttpServletRequest Transfer Encoding Lost – Stack Overflow

不过或许好像可以:

header中设置:

Transfer-Encoding: chunked

然后body中直接选择文件,作为二进制数据发送?

去试试:

然后本地运行Flask,去测试是否正常发送了二进制数据。

<code>  File "/Users/crifan/dev/xxx/app.py", line 828, in post
    speechData = requests.get_data()
AttributeError: module 'requests' has no attribute 'get_data'
127.0.0.1 - - [04/Jul/2018 13:58:08] "POST /asr/language/en-US HTTP/1.1" 500 -
</code>

此处搞错了:

此处用的requests是第三方库

改为:

request.get_data()

但是找不到:request

试试:

<code>from flask import request
speechData = request.get_data()
</code>

结果就可以了。

【总结】

最终用:

<code>class RobotAsrAPI(Resource):

    def post(self, language=settings.ASR_LANGUAGE_DEFAULT):
        log.info("RobotAsrAPI POST: language=%s", language)

        respDict = {
            "code": 200,
            "message": "parse speech ok",
            "data": {
                "speechText": ""
            }
        }

        # speechData = requests.get_data()
        speechData = request.get_data()

        if not speechData:
            respDict["message"] = "Can not get speech binary data!"
            return jsonify(respDict)

        speechDatLen = len(speechData)
        log.info("speechDatLen=%s", speechDatLen)

        # respDict["data"]["speechText"] = doASR(speechData)
        respDict["data"]["speechText"] = "parsed speech text"

        return jsonify(respDict)
</code>

实现了想要的效果。

转载请注明:在路上 » 【已解决】REST工具支持POST发送Transfer-Encoding chunked的二进制数据

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
66 queries in 0.117 seconds, using 18.86MB memory