douleijiang8111 2018-09-17 17:15
浏览 236
已采纳

仅在多个get参数请求中返回第一个参数

I have the following api curl request:

curl --request GET http://127.0.0.1/juris?d=11111&a=22222

Inside my http handler function which has the arguments: w http.ResponseWriter,r *http.Request i have the code:

defer r.Body.Close()
keys,ok := r.URL.Query()["d"]   
if !ok{
 respondWithError(w,http.StatusBadRequest,"InvalidQuery")
 return
}
dnisQuery := string(keys[0])
akeys,aok := r.URL.Query()["a"]     
if !aok{
 respondWithError(w,http.StatusBadRequest,"InvalidQuery")
 return
}
aniQuery := string(akeys[0])

It validates and passes the correct value for the first query parameter d but it fails for the second parameter a with an InvalidQuery

Not sure what im doing incorrectly.

  • 写回答

1条回答 默认 最新

  • dsajdgjadwqe3247382 2018-09-17 17:19
    关注

    If you issue

    curl --request GET http://127.0.0.1/juris?d=11111&a=22222
    

    the shell will see

    curl --request GET http://127.0.0.1/juris?d=11111 & a=22222
    

    and run the first command as a background job, that's why the a part was ignored. (It actually resulted in the shell setting the variable a to 22222.) To get it to work, you have to escape your URL:

    curl --request GET 'http://127.0.0.1/juris?d=11111&a=22222'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵