dtxb75622 2015-07-08 12:52
浏览 445
已采纳

终止或中止HTTP请求

What's the way to abort my API serving with some error message?

Link to call my service:

http://creative.test.spoti.io/api/getVastPlayer?add=
    {"Json":Json}&host=api0.spoti.io&domain=domain&userAgent=userAgent&mobile=true

To call my service the client need to send a Json and some params.

I want to test if the params that I get are correct, if not I want send a error message.

The response should be a Json Code {"Result":"Result","Error":"error message"}

I tried log.fatal and os.Exit(1) they stop the service, not just the call request. panic aborts the call but it prevents me to send a http.ResponseWriter which is my error message.

I read something about panic, defer, recover but I don't really know how can I use them to solve this problem.

return works:

mobile :=query.Get("mobile")
if mobile=="mobile" {
            str:=`{"Resultt":"","Error":"No valide Var"}`
            fmt.Fprint(w, str)      
            fmt.Println("No successfull Operation!!")
            return}  

But I can use it just in the main function, because in the other functions it exits just the func not the caller function (request).

  • 写回答

1条回答 默认 最新

  • douduan5753 2015-07-08 12:57
    关注

    Terminating the serving of an HTTP request is nothing more than to return from the ServeHTTP() method, e.g.:

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        // examine incoming params
        if !ok {
            str := `{"Result":"","Error":"No valide Var"}`
            fmt.Fprint(w, str)
            return
        }
    
        // Do normal API serving
    })
    
    panic(http.ListenAndServe(":8080", nil))
    

    Notes:

    If the input params of your API service are invalid, you should consider returning an HTTP error code instead of the implied default 200 OK. For this you can use the http.Error() function, for example:

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        // examine incoming params
        if !ok {
            http.Error(w, `Invalid input params!`, http.StatusBadRequest) 
            return
        }
    
        // Do normal API serving
    })
    

    For a more sophisticated example where you send back JSON data along with the error code:

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        // examine incoming params
        if !ok {
            w.Header().Set("Content-Type", "application/json")
            w.WriteHeader(http.StatusBadRequest)
            str := `{"Result":"","Error":"No valide Var"}`
            fmt.Fprint(w, str)
            return
        }
    
        // Do normal API serving
    })
    

    Example showing how to propagate "returning"

    If the error is detected outside of ServeHTTP(), e.g. in a function that is called from ServeHTTP(), you have to return this error state so that ServeHTTP() can return.

    Let's assume you have the following custom type for your required parameters and a function which is responsible to decode them from a request:

    type params struct {
        // fields for your params 
    }
    
    func decodeParams(r *http.Request) (*params, error) {
        p := new(params)
        // decode params, if they are invalid, return an error:
    
        if !ok {
            return nil, errors.New("Invalid params")
        }
    
        // If everything goes well:
        return p, nil
    }
    

    Using these:

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        p, err := decodeParams(r)
        if err != nil {
            http.Error(w, `Invalid input params!`, http.StatusBadRequest)
            return
        }
    
        // Do normal API serving
    })
    

    Also see this related question: Golang, how to return in func FROM another func?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建