func (runner *APIRunner) RunSingle(item *ApiItem) error {
	// assign vars

	runner.vm.Assign("url", item.Request.Url)
	// Eval init scope statements
	for _, line := range item.Request.lines {
		runner.vm.EvalStatement(line)
	}

	headers := runner.vm.Lookup("headers").(map[string]funny.Value)

	// send ApiRequest by http client
	res, err := runner.DoAPIRequest(item.Request)
	if err != nil {
		runner.output.ErrorRequest(err)
		return fmt.Errorf("do http request error %s", err.Error())
	}

	buf := new(bytes.Buffer)
	buf.ReadFrom(res.Body)

	item.Response.Headers = res.Header
	item.Response.Status = res.StatusCode
	item.Response.Body = buf.Bytes()

	// collect http response to ApiRequest
	headers = HttpHeaders2VmMap(item.Response.Headers)
	runner.vm.Assign("header", headers)
	runner.vm.Assign("status", item.Response.Status)
	runner.vm.Assign("body", item.Response.Body)

	contentType := item.Response.Headers.Get("Content-Type")
	if strings.HasPrefix(contentType, "application/json") {
		jResults := make(map[string]funny.Value)
		jun := make(map[string]interface{})
		err := json.Unmarshal(item.Response.Body, &jun)
		if err != nil {
			color.Red(fmt.Sprintf("json binding %s %s", err.Error(), item.Response.Body))
		}
		for k, v := range jun {
			jResults[k] = funny.Value(v)
		}
		runner.vm.Assign("json", jResults)

		runner.output.Json(&jResults)
	} else {
		resData, err := ioutil.ReadAll(res.Body)
		if err != nil {
			color.Red(err.Error())
		}
		fmt.Print(string(resData))
	}

	// Eval item response statement
	for _, line := range item.Response.lines {
		runner.vm.EvalStatement(line)
	}

	return nil
}
// RunSingle run the single api item
RunSingle run the single api item
// RunSingle run the single api item
func (runner *APIRunner) RunSingle(item *ApiItem) error {
	runner.vm.Assign("url", item.Request.Url)
	for _, line := range item.Request.lines {
		runner.vm.EvalStatement(line)
	}
	headers := runner.vm.Lookup("headers").(map[string]funny.Value)
	res, err := runner.DoAPIRequest(item.Request)
	if err != nil {
		runner.output.ErrorRequest(err)
		return fmt.Errorf("do http request error %s", err.Error())
	}
	buf := new(bytes.Buffer)
	buf.ReadFrom(res.Body)
	item.Response.Headers = res.Header
	item.Response.Status = res.StatusCode
	item.Response.Body = buf.Bytes()
	headers = HttpHeaders2VmMap(item.Response.Headers)
	runner.vm.Assign("header", headers)
	runner.vm.Assign("status", item.Response.Status)
	runner.vm.Assign("body", item.Response.Body)
	contentType := item.Response.Headers.Get("Content-Type")
	if strings.HasPrefix(contentType, "application/json") {
		jResults := make(map[string]funny.Value)
		jun := make(map[string]interface{})
		err := json.Unmarshal(item.Response.Body, &jun)
		if err != nil {
			color.Red(fmt.Sprintf("json binding %s %s", err.Error(), item.Response.Body))
		}
		for k, v := range jun {
			jResults[k] = funny.Value(v)
		}
		runner.vm.Assign("json", jResults)
		runner.output.Json(&jResults)
	} else {
		resData, err := ioutil.ReadAll(res.Body)
		if err != nil {
			color.Red(err.Error())
		}
		fmt.Print(string(resData))
	}
	for _, line := range item.Response.lines {
		runner.vm.EvalStatement(line)
	}
	return nil
}
func ( runner * APIRunner ) RunSingle ( item * ApiItem ) error { runner . vm . Assign ( " " , item . Request . Url ) for _ , line := range item . Request . lines { runner . vm . EvalStatement ( line ) } headers := runner . vm . Lookup ( " " ) . ( map [ string ] funny . Value ) res , err := runner . DoAPIRequest ( item . Request ) if err != nil { runner . output . ErrorRequest ( err ) return fmt . Errorf ( " " , err . Error ( ) ) } buf := new ( bytes . Buffer ) buf . ReadFrom ( res . Body ) item . Response . Headers = res . Header item . Response . Status = res . StatusCode item . Response . Body = buf . Bytes ( ) headers = HttpHeaders2VmMap ( item . Response . Headers ) runner . vm . Assign ( " " , headers ) runner . vm . Assign ( " " , item . Response . Status ) runner . vm . Assign ( " " , item . Response . Body ) contentType := item . Response . Headers . Get ( " " ) if strings . HasPrefix ( contentType , " " ) { jResults := make ( map [ string ] funny . Value ) jun := make ( map [ string ] interface { } ) err := json . Unmarshal ( item . Response . Body , & jun ) if err != nil { color . Red ( fmt . Sprintf ( " " , err . Error ( ) , item . Response . Body ) ) } for k , v := range jun { jResults [ k ] = funny . Value ( v ) } runner . vm . Assign ( " " , jResults ) runner . output . Json ( & jResults ) } else { resData , err := ioutil . ReadAll ( res . Body ) if err != nil { color . Red ( err . Error ( ) ) } fmt . Print ( string ( resData ) ) } for _ , line := range item . Response . lines { runner . vm . EvalStatement ( line ) } return nil }
RunSingle run the single api item
RunSingle run the single api item
// assign vars // Eval init scope statements // send ApiRequest by http client // collect http response to ApiRequest // Eval item response statement