【Python Flask/postman:request中post的header:Content-type=“text/plain”报错及解决方法】

这篇具有很好参考价值的文章主要介绍了【Python Flask/postman:request中post的header:Content-type=“text/plain”报错及解决方法】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

项目场景:

python Flask构建restful API接口服务,通过postman进行接口服务验证测试


问题描述及解决方法

提示:这里描述项目中遇到的问题:

验证接口调用text/plain时出现报错

源代码(错误):

from flask import Flask,url_for,request,json
app = Flask(__name__)

@app.route("/message",methods=['POST'])
def api_message():
    if request.headers['Content-Type'] =='text/plain':
        return "Text message:" + request.data()#报错1:TypeError: 'bytes' object is not callable
    elif request.headers['Content-Type'] == 'application/json':
        return "Json message: " + json.dumps(request.json)
    elif request.headers['Content-Type'] == 'application/octet-stream':
        f= open("./binary",'wb')
        f.write(request.data)
        f.close()
        return("Binary message written!")
    else:
        return("415 Unsupported Media Type!")
	}

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

报错1: TypeError: ‘bytes’ object is not callable

报错1原因: 类中函数名或者变量名重用(即变量名和函数名有重复)

根据报错1的原因进行修改

return "Text message:" + request.data()#报错1:TypeError: 'bytes' object is not callable  

修改成

return "Text message:" + request.data

但验证接口调用text/plain时还是报错
报错2: TypeError: can only concatenate str (not “bytes”) to str

报错2原因: 两边字符的格式不一致,导致不能用“+”进行拼接
根据报错2的原因进行修改

return "Text message:" + request.data#报错2:TypeError: can only concatenate str (not "bytes") to str  

修改成

return "Text message:" + str(request.data)

修改完成后,接口调用text/plain时不报错,也可以获取request中txt格式的流数据,但是输入123,本该正常输出Text message:123,却输出Text message:b’123’
异常解决方法: 使用bytes类型中的decode()方法,decode()方法完成从比特流向字符串解码的过程

【Python Flask/postman:request中post的header:Content-type=“text/plain”报错及解决方法】【Python Flask/postman:request中post的header:Content-type=“text/plain”报错及解决方法】
根据异常解决方法进行修改

return "Text message:" + str(request.data)

修改成

return "Text message:" + request.data.decode('utf-8')

【Python Flask/postman:request中post的header:Content-type=“text/plain”报错及解决方法】

源代码(正确):文章来源地址https://www.toymoban.com/news/detail-405063.html

from flask import Flask,url_for,request,json
app = Flask(__name__)

@app.route("/message",methods=['POST'])
def api_message():
    if request.headers['Content-Type'] =='text/plain':
        return "Text message:" + request.data.decode('utf-8')
    elif request.headers['Content-Type'] == 'application/json':
        return "Json message: " + json.dumps(request.json)
    elif request.headers['Content-Type'] == 'application/octet-stream':
        f= open("./binary",'wb')
        f.write(request.data)
        f.close()
        return("Binary message written!")
    else:
        return("415 Unsupported Media Type!")
	}

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

到了这里,关于【Python Flask/postman:request中post的header:Content-type=“text/plain”报错及解决方法】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • PHP用CURL发送Content-type为application/json的POST请求方法

    HELLO 各位伙伴,最近一直在做项目,没有及时更新。望请见谅。 今天,给大家讲一下php请求第三方接口的时候遇到的问题,大家都知道,在请求第三方接口的时候,会要求我们用post还是get来传参 一般我们传参的时候基本上都是form-data。有一些第三方为了安全或者是编码格式

    2024年02月10日
    浏览(46)
  • Postman无法将Content-Type从text/plain改为appliation/json

    最近用postman调试接口,默认Headers里面的Content-Type为text/plain,无法修改,输入Request Body相应内容后发送请求,页面报500,如图: 后台提示: org.springframework.web.HttpMediaTypeNotSupportedException: Content type \\\'text/plain;charset=UTF-8\\\' not supported 直接修改Content-Type无法操作,进入postman的Body页面

    2024年02月12日
    浏览(51)
  • postman可以通的请求,前端通不了(前端添加Content-type,后端收不到请求)

    接口完成之后,自己使用postman测试了一下,没有问题; 可是在和小组前端调试接口的时候,他却说访问不了; 信息如下:(我自己写的一个打印请求信息的拦截器) 发现报错信息是:  Content type \\\'application/x-www-form-urlencoded;charset=UTF-8\\\' not supported 也就是说发送过来的内容格式

    2024年02月04日
    浏览(38)
  • Did not attempt to load JSON data because the request Content-Type was not ‘application/json‘

    在使用flask-restfull进行API开发的时候。一旦我使用类似下面的代码从url或者form中获得参数就会出现报错:Did not attempt to load JSON data because the request Content-Type was not ‘application/json’。 代码如下: 解决方法如下

    2024年02月16日
    浏览(58)
  • python中应用requests库模拟postman请求携带token,使用get和post方法请求头携带token

    实际开发中, Python程序中需要调用后台接口 ,充当前端, 后端规定请求头需要携带token postman中 form-data、x-www-form-urlencoded的区别_叫我峰兄的博客-CSDN博客 python requests 带请求头Token发起http请求_python request token_软件测试李同学的博客-CSDN博客 python发送requests请求时,使用登录的

    2024年02月16日
    浏览(43)
  • python实现Content-Type:application/octet-stream

    Content-Type:application/octet-stream 这种传输用于图片|文本传输: 我也是在操作河马云手机中,往云机传输图片和文本txt遇到的。 “”\\\"==============================================================\\\"\\\"\\\" 接下来python实现下: 首先要先安装:requests-toolbelt 图片: 文本:

    2024年02月16日
    浏览(46)
  • 使用Flask.Request的方法和属性,获取get和post请求参数(二)

    在Python发送Post、Get等请求时,我们使用到requests库。Flask中有一个request库,有其特有的一些方法和属性,注意跟requests不是同一个。 用于服务端获取客户端请求数据。注意:是未经任何处理的原始数据而不管内容类型,如果数据时json的,则取得是json字符串,排序和请求参数

    2024年02月13日
    浏览(42)
  • postman能访问,程序不行,报错信息:Message“:“internal error, Unacceptable header specified in request

      在开发过程中发现一个奇怪的现象,同样的ip地址postman能获取到正确的返回,程序中不行,返回的是 error\\\":{\\\"code\\\":\\\"Base.1.0.GeneralError\\\",\\\"@Message.ExtendedInfo\\\":{\\\"Message\\\":\\\"internal error, Unacceptable header specified in request\\\"         解决办法:最后发现是程序中没有设置请求头信息,而postma

    2024年02月12日
    浏览(99)
  • Python爬虫关于网站上传图片: Content-Type: multipart/form-data; boundary=----WebKitFormBoundary****************

    我们在使用python爬虫, 例如使用requests想服务器发送请求,模拟上传图片的时候会遇到Reqest Headers 中有一个:Content-Type: multipart/form-data; boundary=----WebKitFormBoundary****************,  尝试这从其它返回的接口中也找不到它, 我们也一度怀疑是否是在前端JS中生成的,不要着急! 只是

    2024年02月04日
    浏览(42)
  • Postman发送post请求时报400错误,Required request body is missing

    项目形参位置存在@RequestBody注解,用Postman发送post请求时报400错误,Required request body is missing。 错误图示: 解决方法: 方法一: 项目中形参位置不使用@RequestBody,在Postman进行Post请求时,在请求路径后直接拼接参数。 方法二: 项目中形参位置使用@RequestBody,在Postman进行Po

    2024年02月11日
    浏览(58)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包