doutang1946 2016-10-15 03:51
浏览 78
已采纳

在golang中解析带有标点符号的发布数据不正确

I know how to parse post data in golang

r.ParseForm()
pid := r.PostFormValue("pid")
code := r.PostFormValue("code")
lang := r.PostFormValue("lang")
author := r.PostFormValue("author")

But the post data is pid=1&code=#include <stdio.h>\x0Aint main()\x0A{\x0A\x09printf(\x223\x5Cn\x22);\x0A\x09return 0;\x0A}&lang=c&author=11(this is obtained from nginx log)

So when I parse the data, it could be wrong. The parsed data of code is

#include <stdio.h>
int main()
{
    printf("3
")

instead of

#include <stdio.h>
int main()
{
    printf("3
");
    return 0;
}

So how can I fix this problem?

  • 写回答

1条回答 默认 最新

  • douhai9043 2016-10-15 05:32
    关注

    You're passing the raw code, which could be unsafe, your problem is because of this:

    https://golang.org/src/net/url/url.go?s=21047:21092#L761

    enter image description here

    I would suggest that you base64encode your log code and then decode it in your handler.

    import "encoding/base64"
    
    ...
    
    code, err := base64.RawURLEncoding.DecodeString(r.PostFormValue("code"))
        if err != nil {
            // handle error
        }
    

    Then your request should look like this:

    curl --data "pid=1&code=I2luY2x1ZGUgPHN0ZGlvLmg-XHgwQWludCBtYWluKClceDBBe1x4MEFceDA5cHJpbnRmKFx4MjIzXHg1Q25ceDIyKTtceDBBXHgwOXJldHVybiAwO1x4MEF9&lang=c&author=11" http://localhost:8080
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。