Golang JSON/HTTP request like curl -


i looking quick tutorial on how perform requests golang emulate 1 use curl. have 2 apis want communicate both work same way. 1 elasticsearch, other phillips hue. know both of these have libraries in go. that's not i'm after, i'm trying learn how this:

$ curl -xget 'http://localhost:9200/twitter/tweet/_search' -d '{ "query" : {     "term" : { "user" : "kimchy" } } }' 

with golang. can find people seem hard coding

http://url:port/api/_function?something=value?anotherthing=value... 

but have json objects floating around in software. there way can emulate -d feature of curl json string or struct or similar?

as commenter @jimb pointed out, doing request body not disallowed http/1.1 specification; however, not required servers parse body, not surprised if encounter strange behavior.

that said, here how perform request body using golang http client:

reader := strings.newreader(`{"body":123}`) request, err := http.newrequest("get", "http://localhost:3030/foo", reader) // todo: check err client := &http.client{} resp, err := client.do(request) // todo: check err 

the web server see request this:

get /foo http/1.1 host: localhost:3030 user-agent: go 1.1 package http content-length: 12 accept-encoding: gzip  {"body":123} 

to build command-line tool "curl" need use number of go packages (e.g. flag parsing , http request handling) presumably can find need (excellent) docs.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -