Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api Calling in Golang doesn't response in JSON and instead responses in some nonsense Data #40369

Closed
slashramin opened this issue Jul 23, 2020 · 2 comments

Comments

@slashramin
Copy link

@slashramin slashramin commented Jul 23, 2020

What version of Go are you using (go version)?

go version go1.13.3 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64" Output

What did you do?

i'm trying to call a api in Golang and like below i declare my method and other things in order to get the response i require but the problem i'm facing is i don't know if i have a problem in my Request or the response it return have a problem.

this is the function that i use too pass data and call Api

``` func APICall(url string,method string,request string) []byte{ payload := strings.NewReader(request) client := &http.Client { } req, err := http.NewRequest(method, url, payload) if err != nil { fmt.Println(err) } req.Header.Add("Content-Type", "application/json") req.Header.Add("Access", "application/json") res, err := client.Do(req) defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) return body } ``` ====== ``` func Search(c *gin.Context){

url := "https://testapi-v3.iati.ir/Tools/Ping/*************"
method := "POST"
payload := "{"Echo":"asd"}"
resAsByte := APICall(url,method,payload)
fmt.Println(resAsByte)
var v map[string]interface{}
err := json.Unmarshal(resAsByte,&v)
fmt.Println(err)
}

</code>



<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
-->



### doesn't work and this is the binary response that i got

<code>
[171 86 114 77 206 200 87 178 82 74 44 78 81 210 81 242 12 112 76 73 41 74 45 46 6 138 152 152 233 25 153 152 234 25 155 235 25 26 27 43 213 2 0]

</code>



### by the way

err := json.Unmarshal(resAsByte,&v)

@DisposaBoy
Copy link

@DisposaBoy DisposaBoy commented Jul 23, 2020

Code's not formatted correctly, so I didn't read it thoroughly, but it looks like you're printing the bytes returned fmt.Println(resAsByte) instead of the unmarshaled value fmt.Println(v).

@davecheney
Copy link
Contributor

@davecheney davecheney commented Jul 23, 2020

Thank you for raising this issue. Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@davecheney davecheney closed this Jul 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.