sending struct to form field in golang - go

I have a struct which I have to send to an api and it's a post request. But the input are form field. And the fields contains strings, integer, float and image.
I tried to use WriteField function but since this function only takes strings as parameters, I can't process integer and float. How do I do that. Here is my struct and the code snippet.
c := finalObject{
name: Name,
ProfilePic:"/img/unknown.jpg",
owner:"Mr Hall",
latitude:26.5473828,
longitude:88.4249179,
opendays:"Monday-Friday",
openhours:"10am to 5pm",
catId:82,
address:address,
phone_number:2312312,
mobile_number:312312,
email:"dsdas#a.com",
}
url := "https://abcd.com/a"
fmt.Println("URL:>", url)
b, err := json.Marshal(c)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
var jsonStr = []byte(b)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Authorization", "AUTH_TOKEN")
req.Header.Set("enctype", "multipart/form-data")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
fmt.Printf("%#v", c);
}

The Form values in HTTP form are sent as string values only.
If you have the liberty of deciding the form keys as well then you can add the whole structure as a json-encoded string against a generic "data" field and send the request. Else, you'd have to convert the structure values to string representation to send them in request.

Related

How should I pass argument in function in my case?

There is a function that requests a token. I have a problem when I call it again to try to get the token again. In the body I see the following:
{"error":"invalid_request","error_description":"Missing form parameter: grant_type"}
I figured out that the problem lies in passing the payload argument. If I don't pass it, but just copy it inside function, everything is ok. Apparently, it has to be passed in some other way than payload *strings.Reader What's the reason for this behavior of the function and how to correctly pass payload argument into it? I need to pass it, not copy it inside because of reusing this function in my code.
fmt.Println(reflect.TypeOf(payload)) >> *strings.Reader
func getTokenAgain(payload *strings.Reader) string {
//payload := strings.NewReader("grant_type=password&username=admin&password=secret&client_id=admin-cli")
url := "https://my-api-url.com/auth/realms/CP/protocol/openid-connect/token"
method := "POST"
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
log.Fatal(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
accessToken := gjson.Get(string(body), "access_token")
return accessToken.String()
}

How to read from array json response in Go

I have an API request that returns a refresh_token inside array, which looks something like this:
[
{
"refresh_token" : "C61551CEA183EDB767AA506926F423B339D78E2E2537B4AC7F8FEC0C29988819"
}
]
I need to access this refresh_token's value, and use it to query another API.
To do this, I'm attempting to first 'ReadAll' the response body, and then access the key inside of it by calling 'refreshToken'.
However, it's not working. Does anyone know how to resolve this as I can't figure it out?
Here's my code:
func Refresh(w http.ResponseWriter, r *http.Request) {
client := &http.Client{}
// q := url.Values{}
fetchUrl := "https://greatapiurl.com"
req, err := http.NewRequest("GET", fetchUrl, nil)
if err != nil {
fmt.Println("Errorrrrrrrrr")
os.Exit(1)
}
req.Header.Add("apikey", os.Getenv("ENV"))
req.Header.Add("Authorization", "Bearer "+os.Getenv("ENV"))
resp, err := client.Do(req)
if err != nil {
fmt.Println("Ahhhhhhhhhhhhh")
os.Exit(1)
}
respBody, _ := ioutil.ReadAll(resp.Body)
fmt.Println(respBody["refresh_token"])
w.WriteHeader(resp.StatusCode)
w.Write(respBody)
}
If you do not need it as custom type you can cast it as []map[string]string
respBody, _ := ioutil.ReadAll(resp.Body)
var body []map[string]string
json.Unmarshal(respBody, &body)
fmt.Println(body[0]["refresh_token"])

Converting string from HTTP request to data struct in Go

I've got an HTTP Post method, which successfully posts data to an external third party API and returns a response.
I then need data returned from this response to post to my database.
The response contains a few piece of data, but I only need the 'access_token' and 'refresh_token' from it.
As a result, what I'm attempting to do is convert the response from a string into individual components in a new data struct I've created - to then pass to my database.
However, the data is showing as blank, despite it successfully being written to my browser. I'm obviously doing something fundamentally wrong, but not sure what..
Here's my code:
type data struct {
Access_token string `json:"access_token"`
Refresh_token string `json:"refresh_token"`
}
func Fetch(w http.ResponseWriter, r *http.Request) {
client := &http.Client{}
q := url.Values{}
q.Add("grant_type", "authorization_code")
q.Add("client_id", os.Getenv("ID"))
q.Add("client_secret", os.Getenv("SECRET"))
q.Add("redirect_uri", "https://callback-url.com")
q.Add("query", r.URL.Query().Get("query"))
req, err := http.NewRequest("POST", "https://auth.truelayer-sandbox.com/connect/token", strings.NewReader(q.Encode()))
if err != nil {
log.Print(err)
fmt.Println("Error was not equal to nil at first stage.")
os.Exit(1)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request to server")
os.Exit(1)
}
respBody, _ := ioutil.ReadAll(resp.Body)
d := data{}
err = json.NewDecoder(resp.Body).Decode(&d)
if err != nil {
fmt.Println(err)
}
fmt.Println(d.Access_token)
fmt.Println(d.Refresh_token)
w.WriteHeader(resp.StatusCode)
w.Write(respBody)
}
With ioutil.ReadAll you read the body, already. The second time you pass to NewDecoder(resp.Body) the stream was consumed.
You can use instead json.Unmarshal(respBody, &d).
One more advice, don't ignore the error on ioutil.ReadAll

How to send new request with object instead of bytes?

I need to send an object of data e.g. {hello: "world", goodbye: "world"} to an API. I'm doing it like this right now:
inputs := form.GetElementsByTagName("input")
var data = make(map[string]interface{}) // after adding values this looks like this: {hello: "world", goodbye: "world"}
for value := range inputs {
// Append all values from the inputs to a new array, with the key named by the input name attribute
if inputs[value] != nil && inputs[value].(*dom.HTMLInputElement).Value != "" {
data[inputs[value].(*dom.HTMLInputElement).Name] = inputs[value].(*dom.HTMLInputElement).Value
}
}
parsedData, _ := json.Marshal(data)
req, _ := http.NewRequest(method, url, bytes.NewBuffer(parsedData))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
go func() { // Must be a goroutine
response, _ := client.Do(req)
defer response.Body.Close()
}()
The problem I'm having is since we're sending it as a byte, the server always returns error responses as it's expecting to deal with an object.
How can I make sure it's sending an object instead of bytes?
You are setting the content type to application/x-www-form-urlencoded while you are sending the data in json format, so change your content-type when setting the request headers, along with that do not skip the error to check what is the error returned:
parsedData, err := json.Marshal(data)
if err != nil{
fmt.Println(err)
}
req, err := http.NewRequest(method, url, parsedData) // send the parseData which are bytes returned from the marshal.
if err != nil{
fmt.Println(err)
}
req.Header.Set("Content-Type", "application/json") // set the content type to json
go func() { // Must be a goroutine
response, err := client.Do(req)
if err != nil{
fmt.Println(err)
}
defer response.Body.Close()
}()
// you should check for response status to verify the details as
fmt.Println("response Status:", response.Status)
fmt.Println("response Headers:", response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("response Body:", string(body))
One thing that should be taken into consideration is that you have not exported your struct fields. That can be the reason your json string becomes empty. Make your struct fields exportable by making their first letter of each field in caps.
I solved this using the "net/url" package.
data := url.Values{}
for value := range inputs {
// Append all values from the inputs to a new array, with the key named by the input name attribute
if inputs[value] != nil && inputs[value].(*dom.HTMLInputElement).Value != "" {
data.Add(inputs[value].(*dom.HTMLInputElement).Name, inputs[value].(*dom.HTMLInputElement).Value)
}
}
req, _ := http.NewRequest(method, actionUrl, strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

Sending octet stream

I have two go programs - one running as a server daemon, the other being executed manually. I want to be able to send a request to the server from the other program, sending some binary data there via post request. How can I do this?
I know I can send a string like this:
data := url.Values{}
data.Set("req", buf)
u, _ := url.ParseRequestURI(domain)
u.Path = path
urlStr := fmt.Sprintf("%v", u)
client := &http.Client{}
r, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))
r.Header.Add("Authorization", "auth_token=\"XXXXXXX\"")
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
resp, _ := client.Do(r)
return resp.Status
But I want to send an octet-stream which I can then read from ioutil.ReadAll(r.Body).
The following show how to send data inside the request body to a server, and read it on the server side. The client part is as follow:
c := http.Client{}
data := []byte("This is a content that will be sent in the body")
r, err := http.NewRequest("POST", "http://localhost:8080", bytes.NewBuffer(data))
// You should never ignore the error returned by a call.
if err != nil {
panic(err)
}
c.Do(r)
And in your http.Handler function:
d, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
fmt.Println("Request content : ", string(d))
This is the easiest way.

Resources