Getting backlashes in GET response

Hello,

I am new with Go. I have reviewed the documentation on how to make http.ResponseWriter work. I built a GET request to an API that looks like the following:

    ```func main() {
    	router := mux.NewRouter()
    	router.HandleFunc("/gomk/fixall/{server}", handler).Methods("GET")
    	headers := handlers.AllowedHeaders([]string
        {"X-Requested-With", "Content-Type", "Authorization", 
        "access-control-allow-headers", "access-control-allow-methods",
         "access-control-allow-origin"})
    	methods := handlers.AllowedMethods([]string
        {"GET", "POST", "PUT", "DELETE", "OPTIONS"})
    	origins := handlers.AllowedOrigins([]string{"*"})

    	fmt.Println("Server is running at port 8000")
    	log.Fatal(http.ListenAndServe(":8000", handlers.CORS(headers, 
        methods, origins)(router)))
    }

    func handler(w http.ResponseWriter, r *http.Request) {
    	params := mux.Vars(r)
    	server := params["server"]`
    resp, err := http.Get(`https://test.com/api/test&request={"hostname":"` 
    + params["server"] + `","mode":"test"}`)
    	if err != nil {
    		log.Fatalln(err)
    		return
    	}

    	defer resp.Body.Close()

    	body, err := ioutil.ReadAll(resp.Body)
    	if err != nil {
    		log.Fatalln(err)
    	}

    	json.NewEncoder(w).Encode(string(body))
    	fmt.Println(string(body))
    }```

The call does return the correct information in the browser at port 8000: however, I am getting the following backslashes in the response:

"{\"result\": \"

Where it actually should be:

{"result": "

The correct output without the backlashes does show in the console output. Can somebody please help with the reason why I am getting those backlashes in the browser and what type of configuration would help me receive the output that I am looking for?

You json encode a string and not an object. Maybe you should just write the string from the api as it is?

ok. Will investigate on that. Just to clarify my post, currently in the console output I do receive {"result": " without the backlashes (just corrected my post).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.