drtiwd06558 2016-05-09 10:03 采纳率: 100%
浏览 471

缓冲区问题不能在http.NewRequest golang的参数中将<type>用作类型io.Reader

I was having an issue when I was passing string in http.NewRequest in golang body param.

The error I got was :

cannot use req.Body (type string) as type io.Reader in argument to http.NewRequest: string does not implement io.Reader (missing Read method)

Similarly there are other use cases where the Buffer is required as an input instead of a particular type or an array of it. eg, byte[] when input required is buffer.

What did the error mean and what are ways of solving it and understanding what golang is trying to enforce.

Edit: This was the line I was having an issue with and did not find any references.

http.NewRequest(req.Method, req.Url, req.Body)

http.NewRequest(req.Method, req.Url, strings.NewReader(req.Body)) solves the issue. I was planning to add the answer as well (as an FYI type of question)

  • 写回答

1条回答 默认 最新


    相关推荐 更多相似问题


    • ¥20 有没有认识硅云的研发 求介绍
    • ¥15 unittest __import__加载名为‘1’的model无法找到
    • ¥30 魔术师读心术猜数字实验
    • ¥15 关于#Python#的问题,如何解决?
    • ¥15 CMD 生成TXT内容输出成一行
    • ¥20 关于#pandas#的问题,如何解决?(语言-python)
    • ¥30 c语言设计井字棋小游戏
    • ¥15 关于属性关联的双极容度多属性决策vikor方法的公式
    • ¥15 10吨190马力爆炸品运输车,以80km/h的速度行驶1个小时输出的动能或者做的功是多少?
    • ¥15 《phthon编程从入门到实践》第十八章,从django入手,learning_log,ModuleNotFoundError: No module named 'learning_logs