// DoRequest executes LastPass specific request in JSON format and returns http Response
DoRequest executes LastPass specific request in JSON format and returns http Response
// DoRequest executes LastPass specific request in JSON format and returns http Response
func (c *LastPassClient) DoRequest(command string, payload interface{}) (*http.Response, error) {
	data := struct {
		CompanyID        string      `json:"cid"`
		ProvisioningHash string      `json:"provhash"`
		Command          string      `json:"cmd"`
		Payload             interface{} `json:"data"`
	}{
		CompanyID:        c.CompanyID,
		ProvisioningHash: c.APIKey,
		Command:          command,
	}
	if payload != nil {
		data.Payload = payload
	}
	body, err := JSONReader(data)
	if err != nil {
		return nil, err
	}
	req, err := http.NewRequest(http.MethodPost, c.URL.String(), body)
	if err != nil {
		return nil, err
	}
	req.Header.Add("Content-Type", "application/json")
	for header, values := range c.Headers {
		for _, v := range values {
			req.Header.Add(header, v)
		}
	}
	req.Header.Set("X-Api-Key", c.APIKey)
	req.Header.Set("User-Agent", c.UserAgent)
	if c.Verbose {
		dump, err := httputil.DumpRequest(req, true)
		if err == nil {
			log.Printf("%s", dump)
		}
	}
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	if c.Verbose {
		dump, err := httputil.DumpResponse(resp, true)
		if err == nil {
			log.Printf("%s", dump)
		}
	}
	if resp.StatusCode < 200 || resp.StatusCode > 299 {
		return resp, fmt.Errorf("API result failed: %s\n", resp.Status)
	}
	return resp, err
}
func ( c * LastPassClient ) DoRequest ( command string , payload interface { } ) ( * http . Response , error ) { data := struct { CompanyID string `json:"cid"` ProvisioningHash string `json:"provhash"` Command string `json:"cmd"` Payload interface { } `json:"data"` } { CompanyID : c . CompanyID , ProvisioningHash : c . APIKey , Command : command , } if payload != nil { data . Payload = payload } body , err := JSONReader ( data ) if err != nil { return nil , err } req , err := http . NewRequest ( http . MethodPost , c . URL . String ( ) , body ) if err != nil { return nil , err } req . Header . Add ( " " , " " ) for header , values := range c . Headers { for _ , v := range values { req . Header . Add ( header , v ) } } req . Header . Set ( " " , c . APIKey ) req . Header . Set ( " " , c . UserAgent ) if c . Verbose { dump , err := httputil . DumpRequest ( req , true ) if err == nil { log . Printf ( " " , dump ) } } resp , err := http . DefaultClient . Do ( req ) if err != nil { return nil , err } if c . Verbose { dump , err := httputil . DumpResponse ( resp , true ) if err == nil { log . Printf ( " " , dump ) } } if resp . StatusCode < 200 || resp . StatusCode > 299 { return resp , fmt . Errorf ( " \n " , resp . Status ) } return resp , err }
DoRequest executes LastPass specific request in JSON format and returns http Response
DoRequest executes LastPass specific request in JSON format and returns http Response
// Form body. // Form Request. // Add Header. // Do Request // TODO: This will not work because LastPass Provisioning API always return `Content-Type: text/xml;charset=utf-8` regardless of the API result. // HELP NEEDED //if strings.Contains(resp.Header.Get("Content-Type"), "text/xml") { //	var XML struct { //		Message string `xml:"rc,attr"` //	} //	b := io.ReadCloser(resp.Body) //	err := xml.NewDecoder(b).Decode(&XML) //	if err != nil { //		return resp, fmt.Errorf("API result failed: %v\n", XML.Message) //	} //}