cURL / HTTPS PUT Request

3,515 views
Skip to first unread message

Christian Schmitt

unread,
Apr 30, 2012, 4:27:51 PM4/30/12
to golan...@googlegroups.com
Hello, I got a HTTPS URL and I wanted to PUT some data to it.

With cURL I could easily cURL it with:
curl -X PUT -d status=START -d 'planid=10' https://api.jiffybox.de/<Token>/v1.0/jiffyBoxes/12345
Now I wanted to do this with Golang, cause I wanted to make this automatic.
I searched the HTTP Library but I didn't find anything to make a HTTPS Request, so how could I do it?

Kyle Lemons

unread,
Apr 30, 2012, 4:51:18 PM4/30/12
to Christian Schmitt, golan...@googlegroups.com
1.  Create a string reader with your data (or some other io.Reader) http://golang.org/pkg/strings/#NewReader encoded from the form Values http://golang.org/pkg/net/url/#Values.Encode (or just hand-code it)
2.  Make a new PUT request http://golang.org/pkg/net/http/#NewRequest with the reader as the body
3.  Execute the request with the http.DefaultClient http://golang.org/pkg/net/http/#Client.Do

Volker Dobler

unread,
Apr 30, 2012, 4:53:03 PM4/30/12
to golang-nuts


On Apr 30, 10:27 pm, Christian Schmitt <c.schm...@briefdomain.de>
wrote:
> I searched the HTTP Library but I didn't find anything to make a HTTPS
> Request, so how could I do it?

Did you try something like (untested pseudocode):
request, err := http.NewRequest("PUT", url, body)
response, err := http.DefaultClient.Do(request)
?

V

Christian Schmitt

unread,
Apr 30, 2012, 5:28:45 PM4/30/12
to golan...@googlegroups.com
Yeah I got this already:



package main

import "fmt"
import "net/http"

import "io/ioutil"


func main() {



client := &http.Client{}



req, _ := http.NewRequest("PUT", "https://api.jiffybox.de/HASH/v1.0/jiffyBoxes/ID", nil)


req.FormValue := "status=SHUTDOWN"
resp, _ := client.Do(req)

out, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()

fmt.Printf("%s", out)




}

Still I didn't need to know how to send the Data like "status=SHUTDOWN" or in json {"key1":value1} I only know how to make a request not how to send data.. that is my problem i know i need something with io.Reader but don't know how to make it correctly

Christian Schmitt

unread,
Apr 30, 2012, 5:35:50 PM4/30/12
to golan...@googlegroups.com
req.FormValue("status=SHUTDOWN") didn't work cause it will be ignored. but i don't know how to form a io.Reader with a string like "status=SHUTDOWN" or a json string. 

Kyle Lemons

unread,
Apr 30, 2012, 5:56:35 PM4/30/12
to Christian Schmitt, golan...@googlegroups.com
Did you read my post?

Christian Schmitt

unread,
Apr 30, 2012, 6:50:51 PM4/30/12
to golan...@googlegroups.com, Christian Schmitt
Oh my god.. No... Sorry dude..
it works know -.- missed the string.Reader() my solution was so close, but i overreaded you and the string reader...
solutions can be so easy..

Christian Schmitt

unread,
Apr 30, 2012, 6:55:39 PM4/30/12
to golan...@googlegroups.com, Christian Schmitt
Another question.. I got a json as a response, but its not byte-encoded could i make it byte-encoded and decode it via unmarshal?

Kyle Lemons

unread,
Apr 30, 2012, 7:10:54 PM4/30/12
to Christian Schmitt, golan...@googlegroups.com
The response includes a resp.Body which can be directly used via json.NewDecoder(resp.Body).Decode(&obj)

Brad Fitzpatrick

unread,
Apr 30, 2012, 7:26:13 PM4/30/12
to Christian Schmitt, golan...@googlegroups.com
What do you mean by "byte-encoded"?

Christian Schmitt

unread,
Apr 30, 2012, 7:27:16 PM4/30/12
to golan...@googlegroups.com, Christian Schmitt
Ah yeah thanks got it. Golang is very easy.. some things should be described in my native language, then it would be easier but its okai.. i like the language. maps are amazing.

Stephen Weinberg

unread,
Apr 30, 2012, 4:52:04 PM4/30/12
to golang-nuts
HTTPS is supported by default. Just give it an https:// address and it
will use SSL.

You can do a PUT by setting the Method string in your request.
http://golang.org/pkg/net/http/#Request

-- Stephen

On Apr 30, 1:27 pm, Christian Schmitt <c.schm...@briefdomain.de>
wrote:
Reply all
Reply to author
Forward
0 new messages