mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 10:12:37 +02:00
add cookie and revamp post
This commit is contained in:
parent
262737a66d
commit
3bc595757c
@ -3,9 +3,9 @@ package piwigo
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p *Piwigo) BuildUrl(method string) (string, error) {
|
func (p *Piwigo) BuildUrl(method string) (string, error) {
|
||||||
@ -22,29 +22,34 @@ func (p *Piwigo) BuildUrl(method string) (string, error) {
|
|||||||
return Url.String(), nil
|
return Url.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Piwigo) Post(method string, req *url.Values, resp interface{}) error {
|
func (p *Piwigo) Post(method string, form *url.Values, resp interface{}) error {
|
||||||
Url, err := p.BuildUrl(method)
|
Url, err := p.BuildUrl(method)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := http.PostForm(Url, *req)
|
req, err := http.NewRequest("POST", Url, strings.NewReader(form.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
if p.Token != "" {
|
||||||
|
req.AddCookie(&http.Cookie{Name: "pwg_id", Value: p.Token})
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
b, err := ioutil.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
Result := PiwigoResult{
|
Result := PiwigoResult{
|
||||||
Result: resp,
|
Result: resp,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(b, &Result)
|
err = json.NewDecoder(r.Body).Decode(&Result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user