On Tue, Nov 13, 2018, 5:23 AM <alive.s...@gmail.com> wrote:

> I just want to send a rest call with golang, but it failed, the error
> message :  {"header":{"code":201,"desc":"param data not exist"}} .    (but
> when I send rest request with postman ,it success)
> the code as follow
>
> 1 package main
>   2
>   3 import(
>   4         "net/http"
>   5         "fmt"
>   6         "strings"
>   7         "io/ioutil"
>   8 )
>   9 func check(err error){
>  10         if err !=nil{
>  11                 panic(err)
>  12         }
>  13 }
>  14 func main(){
>  15         url:="http://ip:8080/api";
>  16         method:="POST"
>  17         da:="base64 encoded string"
>  18         data:=strings.NewReader(da)
>

This isn't an encoded form. And your server is complaining that it can't
find a "data" form field. Use url.Values to encode a form:
https://golang.org/pkg/net/url/#Values.Encode

var vals url.Values
vals.Add("data", "form data")
data:=strings.NewReader(vals.Encode())

 19         req,err:=http.NewRequest(method,url,data)
>  20         check(err)
>  21
>  req.Header.Set("Content_Type","application/x-www-form-urlencoded")
>


Should be "Content-Type"

 22         client:=&http.Client{}
>  23         resp,err:=client.Do(req)
>  24         check(err)
>  25         defer resp.Body.Close()
>  26         result,err:=ioutil.ReadAll(resp.Body)
>  27         check(err)
>  28         fmt.Println(string(result[:]))
>  29 }
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to