duanbo2048 2016-09-02 22:52
浏览 43

弹性beantalk上的Golang应用似乎正在接收双重编码的请求

I'm using gorilla/mux inside a Golang application to retrieve variables out of my routes, like so:

func main() {
  router := mux.NewRouter()
  router.HandleFunc("/items/{name}", itemHandler)
  log.Fatal(http.ListenAndServe(":5000", router)
}

func itemHandler(w http.ResponsWriter, r *http.Request) {
  name := mux.Vars(r)["name"]
  fmt.Println("name is: ", name)
}

If I navigate to /items/super%20duper on my local VM the console output is name is: super duper, as I would expect. But when I run this on our Elastic Beanstalk instance and go to the same URL the console output is name is: super%20duper.

We tried changing the proxy_pass entry in the nginx config thinking maybe nginx was not passing the request URI exactly as received, but that had no effect.

If anyone else has seen the same issue I would love to know how you solved it.

  • 写回答

1条回答 默认 最新

  • douzangdang2225 2016-09-03 03:42
    关注

    You may use func QueryUnescape(s string) (string, error) from "net/url" package:

    QueryUnescape:

    QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.

    package main
    
    import (
        "fmt"
        "net/url"
    )
    
    func main() {
        s, err := url.QueryUnescape("super%20duper")
        if err != nil {
            panic(err)
        }
        fmt.Println(s) // super duper
    }
    

    output:

    super duper
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP