How to access http client response body global [closed] - http

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
In the code below, am trying to access the variable "Regprofile" as global variable but and getting empty output. Any help?
type GMLCInstance struct {
NfInstanceID string `json:"nfInstanceID"`
HeartBeatTimer int `json:"heartBeatTimer"`
NfType []string `json:"nfType"`
NfStatus []string `json:"nfStatus"`
Ipv4Addresses []string `json:"ipv4Addresses"`
}
var Regprofile GMLCInstance
// Request credentials and token from NRF and register profile to NFR database
func init() {
urlcred := "https://127.0.0.1:9090/credentials"
// Perform the request
resp, err := netClient.Get(urlcred)
if err != nil {
log.Fatalf("Failed get: %s", err)
}
defer resp.Body.Close()
// Fill the record with the data from the JSON
var cr Credential
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(resp.Body).Decode(&cr); err != nil {
log.Println(err)
}
//fmt.Println(cr)
clientId := cr.CLIENTID
clientsec := cr.CLIENTSECRET
// Get token
reqtoken := url.Values{
"grant_type": []string{"client_credentials"},
"client_id": []string{clientId},
"client_secret": []string{clientsec},
"scope": []string{"GMLC"},
}
urlq := "https://127.0.0.1:9090/oauth2/token?"
res, err := netClient.PostForm(urlq+reqtoken.Encode(), nil)
if err != nil {
log.Fatalf("Failed get: %s", err)
}
var auth AccessToken
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(res.Body).Decode(&auth); err != nil {
log.Println(err)
}
//fmt.Println(auth.AccessToken)
token := auth.AccessToken
para := url.Values{
"access_token": []string{token},
}
//============================================================
// Register GMLC Instance to NRF, PUT
var gmlc = []byte(`{
"nfInstanceID": "f81d4fae-7dec-11d0-a765-00a0c91egmlc",
"heartBeatTimer": 0,
"nfType": ["GMLC"],
"nfStatus": ["string"],
"ipv4Addresses": ["172.16.0.X:5000"]
}`)
//================= Registser profile to NRF ========================//
postnrf := "https://127.0.0.1:9090/nnrf-nfm/v1/nf-instances/f81d4fae-7dec-11d0-a765-00a0c91egmlc?"
rq, err := http.NewRequest("PUT", postnrf+para.Encode(), bytes.NewBuffer(gmlc))
rq.Header.Set("Content-Type", "application/json")
rq.Header.Set("Accept", "application/json")
response, err := netClient.Do(rq)
if err != nil {
panic(err)
}
defer res.Body.Close()
decoder := json.NewDecoder(response.Body)
var Regprofile GMLCInstance
err = decoder.Decode(&Regprofile)
if err != nil {
fmt.Println("Response body not well decoded")
}
fmt.Println(Regprofile)
}
The output of fmt.Println(Regprofile) gives
{f81d4fae-7dec-11d0-a765-00a0c91egmlc 10 [GMLC] [REGISTERED] [172.16.0.X:5000]}
However, when i print the variable in for example main as
func main(){
fmt.Println(Regprofile)
}
I get empty data as
{ 0 [] [] []}

In func init() you redeclare variables var Regprofile GMLCInstance locally to function scope. This declaration shadows global variable with local one. Just delete this local declaration inside init().

Related

Sending form-data in a POST request in golang

I'm trying to send form-data by making a post request. The api works fine (I've tested on postman), but I'm not sure why I'm having trouble to do it in golang. The form-data contains a task field and a file field. But if I do the following I get Bad Request. Any ideas why I might be getting this? Thanks in advance.
// Create new buffer and writer
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
// read data from file
var fdata []byte
if fd, e := os.Open(pdf); e != nil {
log.Fatal(e)
} else {
fd.Read(fdata)
}
// create file field and write
part, err := w.CreateFormFile("file", pdf)
if err != nil {
log.Fatal(err)
}
part.Write(fdata)
// create the task field and write
part, err = w.CreateFormField("task")
if err != nil {
log.Fatal(err)
}
part.Write([]byte(os.Getenv("task")))
w.Close()
// Create a new request
req, err := http.NewRequest("POST", fmt.Sprintf("https://%v/v1/upload",os.Getenv("server")), buf)
// Set content type header
req.Header.Add("Content-Type", "multipart/form-data")
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// other stuff

Sending data in Chunks using single HTTP Post connection

I receive the contents of a file from a data source in chunks. As and when I receive the chunk I want to send the chunk data to a service using http POST request. And by keeping alive the same http POST connection used for sending the first chunk I want to send the remaining chunks of data.
I came up with the following code snippet to implement something similar.
Server-Side
func handle(w http.ResponseWriter, req *http.Request) {
buf := make([]byte, 256)
var n int
for {
n, err := req.Body.Read(buf)
if n == 0 && err == io.EOF {
break
}
fmt.Printf(string(buf[:n]))
}
fmt.Printf(string(buf[:n]))
fmt.Printf("Transfer Complete")
}
Client-Side
type alphaReader struct {
reader io.Reader
}
func newAlphaReader(reader io.Reader) *alphaReader {
return &alphaReader{reader: reader}
}
func (a *alphaReader) Read(p []byte) (int, error) {
n, err := a.reader.Read(p)
return n, err
}
func (a *alphaReader) Reset(str string) {
a.reader = strings.NewReader(str)
}
func (a *alphaReader) Close() error {
return nil
}
func main() {
tr := http.DefaultTransport
alphareader := newAlphaReader(strings.NewReader("First Chunk"))
client := &http.Client{
Transport: tr,
Timeout: 0,
}
req := &http.Request{
Method: "POST",
URL: &url.URL{
Scheme: "http",
Host: "localhost:8080",
Path: "/upload",
},
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: -1,
Body: alphareader,
}
fmt.Printf("Doing request\n")
_, err := client.Do(req)
alphareader.Reset("Second Chunk")
fmt.Printf("Done request. Err: %v\n", err)
}
Here I want that when I do alphareader.Reset("Second Chunk"), the string "Second Chunk" should be sent using the POST connection made earlier. But that is not happening. The connection gets closed after sending the First Chunk of data. Also I have not written the Close() method properly which I'm not sure how to implement.
I'm newbie to golang and any suggestions would be greatly helpful regarding the same.
A *strings.Reader returns io.EOF after the initial string has been read and your wrapper does nothing to change that, so it cannot be reused. You're looking for io.Pipe to turn the request body into an io.Writer.
package main
import (
"io"
"net/http"
)
func main() {
pr, pw := io.Pipe()
req, err := http.NewRequest("POST", "http://localhost:8080/upload", pr)
if err != nil {
// TODO: handle error
}
go func() {
defer pw.Close()
if _, err := io.WriteString(pw, "first chunk"); err != nil {
_ = err // TODO: handle error
}
if _, err := io.WriteString(pw, "second chunk"); err != nil {
_ = err // TODO: handle error
}
}()
res, err := http.DefaultClient.Do(req)
if err != nil {
// TODO: handle error
}
res.Body.Close()
}
Also, don't initialize the request using a struct literal. Use one of the constructors instead. In your code you're not setting the Host and Header fields, for instance.

Golang *bytes.Buffer nil causes fatal error [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I had the same issue as https://github.com/golang/go/issues/26666 because I have a wrap function for my http requests.
Sometimes I need to request:
body := new(bytes.Buffer)
json.NewEncoder(body).Encode(h)
req("POST", "http://example.com", body)
And sometimes it's simply:
req("GET", "http://example.com", nil)
runtime error: invalid memory address or nil pointer dereference
I ended up with:
req("GET", "http://example.com", new(bytes.Buffer))
But I'm not sure if it's the right thing to do.
The function:
func req(method string, url string, body *bytes.Buffer) int {
req, err := http.NewRequest(method, url, body)
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(user, psw)
resp, err := client.Do(req)
checkErr(err)
if resp.StatusCode > 500 {
time.Sleep(30 * time.Second)
resp, err = client.Do(req)
checkErr(err)
}
defer resp.Body.Close()
return resp.StatusCode
}
Updated function:
func req(method string, url string, body io.Reader) int {
req, err := http.NewRequest(method, url, body)
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(user, psw)
resp, err := client.Do(req)
checkErr(err)
defer resp.Body.Close()
if resp.StatusCode >= 500 {
time.Sleep(30 * time.Second)
req, err := http.NewRequest(method, url, body)
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(user, psw)
resp, err := client.Do(req)
checkErr(err)
defer resp.Body.Close()
}
return resp.StatusCode
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
The body in http.NewRequest() is optional, so passing nil is acceptable when you're doing GET requests.
The problem is that the body parameter of http.NewRequest is an interface type: io.Reader, and you're attempting to pass a value of a concrete type *bytes.Buffer. What happens is that this nil pointer will be wrapped in a non-nil interface value, and that will be passed to http.NewRequest as the body.
If you don't have a body, pass nil explicitly, like this:
func req(method string, url string, body *bytes.Buffer) int {
var bodyToPass io.Reader
if body != nil {
bodyToPass = body
}
req, err := http.NewRequest(method, url, bodyToPass)
// ....
}
And then you can call it like:
req("GET", "http://example.com", nil)
Although best would be if your req() function would take io.Reader in the first place, so you don't have to check its value explicitly:
func req(method string, url string, body io.Reader) int {
req, err := http.NewRequest(method, url, body) // You may pass it as-is
// ....
}
And you can call it with nil or with a non-nil *bytes.Buffer too:
req("GET", "http://example.com", nil) // OK
req("POST", "http://example.com", bytes.NewBufferString("data")) // Also OK
For more details, see Hiding nil values, understanding why golang fails here

Golang Struct as Payload for POST Request

New to golang. I'm trying to make a POST request to an auth endpoint to get back a token for authing further requests. Currently the error I'm getting is missing "credentials". I've written the same logic in Python so I know what I am trying to do is what the system is expecting.
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"os"
)
type Auth struct {
Method string `json:"credentials"`
Email string `json:"email"`
Password string `json:"password"`
Mfa string `json:"mfa_token"`
}
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Email: ")
e, _ := reader.ReadString('\n')
fmt.Print("Enter Password: ")
p, _ := reader.ReadString('\n')
fmt.Print("Enter 2FA Token: ")
authy, _ := reader.ReadString('\n')
auth := Auth{"manual", e, p, authy}
j, _ := json.Marshal(auth)
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
}
req, err := http.NewRequest("POST", "https://internaltool.com/v3/sessions", bytes.NewBuffer(j))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Accept-Encoding", "gzip, deflate, br")
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
s := string(body)
if res.StatusCode == 400 {
fmt.Println("Bad Credentials")
fmt.Println(s)
return
}
}
The question is - am I properly marshalling the AUTH struct into JSON and adding it appropriately to the POST request? As the API is not even seeing the credentials key in the JSON I think I must be doing something wrong. Anything helps.
Here's a minimum viable example of using json.Marshal to convert a Struct to a JSON object in the context of a POST request.
Go's standard libraries are fantastic, there is no need to pull in external dependencies to do such a mundane thing.
func TestPostRequest(t *testing.T) {
// Create a new instance of Person
person := Person{
Name: "Ryan Alex Martin",
Age: 27,
}
// Marshal it into JSON prior to requesting
personJSON, err := json.Marshal(person)
// Make request with marshalled JSON as the POST body
resp, err := http.Post("https://httpbin.org/anything", "application/json",
bytes.NewBuffer(personJSON))
if err != nil {
t.Error("Could not make POST request to httpbin")
}
// That's it!
// But for good measure, let's look at the response body.
body, err := ioutil.ReadAll(resp.Body)
var result PersonResponse
err = json.Unmarshal([]byte(body), &result)
if err != nil {
t.Error("Error unmarshaling data from request.")
}
if result.NestedPerson.Name != "Ryan Alex Martin" {
t.Error("Incorrect or nil name field returned from server: ", result.NestedPerson.Name)
}
fmt.Println("Response from server:", result.NestedPerson.Name)
fmt.Println("Response from server:", result.NestedPerson.Age)
}
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
// NestedPerson is the 'json' field of the response, what we originally sent to httpbin
type PersonResponse struct {
NestedPerson Person `json:"json"` // Nested Person{} in 'json' field
}
As http.Client is relatively a low-level abstraction, gorequest(https://github.com/parnurzeal/gorequest) as an alternative is strongly recommended.
headers, queries, and body can be posted in any type, which is a bit more like what we often do in Python.

How i can update data firebase with specific key in golang?

I am using golang and firego for connecting to Firebase. I want to update my data Statusfrom ON to OFF with key IDAgent: 7. This is my Database Structure
Image
Assumption : I don't know child active_chat. How can i update data in active_chat/-Koja8GuFplEN3kjbfPO where IDAgent = 7
I have tried this code
x := map[string]string{"Status": "OFF"}
ref.OrderBy("IDAgent").EqualTo("7").Update(x)
but this code wrong query.
In two ways you can do, as per Firebase doc with firego client library. Drafted answer based on from firego README.md.
Note: You have not provided the complete path of the structure, I have drafted the answer based on screenshot. So update your JSON path accordingly.
Approach 1:
f := firego.New("https://my-firebase-app.firebaseIO.com/active-chat/Koja8GuFpIEN3kjbfPO.json", nil)
x := map[string]string{
"Status": "OFF",
}
if err := f.Update(x); err != nil {
log.Fatal(err)
}
Approach 2:
f := firego.New("https://my-firebase-app.firebaseIO.com", nil)
f = f.Ref("/active-chat/Koja8GuFpIEN3kjbfPO.json")
x := map[string]string{
"Status": "OFF",
}
if err := f.Update(x); err != nil {
log.Fatal(err)
}
Update for 2022:
package main
import (
"context"
"fmt"
"time"
firestore "cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
"google.golang.org/api/option"
)
type (
myDocument struct {
Cars []Car `firestore:"cars"`
carsCount int64 `firestore:"car_count"`
UpdateTime string `firestore:"update_time"`
}
Car struct {
Name string `firestore:"name"`
YearBuilt string `firestore:"year_built"`
}
)
func getFirebaseClient(ctx context.Context) (*firestore.Client, error) {
sa := option.WithCredentialsFile("Path_To_Firebase_Key")
// Initialize firebase app with admin privileges
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
err = fmt.Errorf("getFirestoreClient failed: %s", err)
return nil, err
}
// Create client
client, err := app.Firestore(ctx)
if err != nil {
err = fmt.Errorf("failed to connect to firestore: %v", err)
return nil, err
}
return client, nil
}
func main() {
// Create context
ctx := context.Background()
// Get firebase client
client, err := getFirebaseClient(ctx)
if err != nil {
panic(err)
}
// Create car struct
newCar := Car{
"Volvo_Series1",
"1920",
}
// Update time
newTime := time.Now().UTC().Format("Monday, 01-02-2006 15:04:05")
// Updates to document
updates := []firestore.Update{
{Path: "cars", Value: firestore.ArrayUnion(newCar)},
{Path: "car_count", Value: firestore.Increment(1)},
{Path: "update_date", Value: newTime},
}
// OPTION A)
// Create collection reference
collectionRef := client.Collection("cars")
// Create document reference
docRef := collectionRef.Doc("12345")
// Update document
_, err = docRef.Update(ctx, updates)
if err != nil {
err := fmt.Errorf("failed updating document: %s from %s collection %v", docRef.ID, docRef.Parent.ID, err)
panic(err)
}
// OPTION B)
_, err = client.Collection("cars").Doc("12345").Update(ctx, updates)
if err != nil {
err := fmt.Errorf("failed updating document: %s from %s collection %v", docRef.ID, docRef.Parent.ID, err)
panic(err)
}
}

Resources