Ejemplo n.º 1
0
func (c Servers) Encode(serverId string, apiParamId string) revel.Result {
	server := models.SelectServerOneByServerId(serverId)
	if server.ServerId == "" {
		return c.ErrInputData(MsgSeleteError)
	}

	apiParam := models.ParseApi(server.Content, apiParamId)
	// values.Del("Host")
	// values.Del("Method")
	// json.Unmarshal([]byte(api), &apiParam)
	return c.RenderJsonSucWithData(map[string]interface{}{
		"resp": apiParam.Encode(c.Params.Form),
	})
}
Ejemplo n.º 2
0
func (c Servers) Active(serverId string, apiParamId string, apiParam models.ApiParam) revel.Result {
	if c.IsGet() {
		server := models.SelectServerOneByServerId(serverId)
		if server.ServerId == "" {
			return c.ErrDataBase(MsgSeleteError)
		}

		apiParam := models.ParseApi(server.Content, apiParamId)
		return c.Render(apiParam, serverId, apiParamId)
	} else {
		var values = c.Params.Form
		var location = values["location[]"]
		values.Del("Host")
		values.Del("Method")
		values.Del("location[]")
		req, err := http.NewRequest(apiParam.Method, apiParam.Host, strings.NewReader(values.Encode()))
		if err != nil {
			revel.ERROR.Println("http req err =>", err)
			return c.RenderJsonErrWithData(map[string]interface{}{
				"resp": "错误" + err.Error(),
			})
		}
		for i := 0; i < len(location); i = i + 1 {
			if value := values.Get(location[i]); value != "" {
				req.Header.Add(location[i], value)
			}
		}
		client := &http.Client{
			Timeout: 10 * time.Second,
		}
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded; ")
		resp, err := client.Do(req)
		if err != nil {
			revel.ERROR.Println("http resp err =>", err)
			return c.RenderJsonErrWithData(map[string]interface{}{
				"resp": "错误" + err.Error(),
			})
		}
		// // client := &http.Client{Transport: transport}
		robots, _ := ioutil.ReadAll(resp.Body)
		return c.RenderJsonSucWithData(map[string]interface{}{
			"resp": string(robots),
		})
	}
}