Convert python code to Go for json encoding

huangapple go评论126阅读模式
英文:

Convert python code to Go for json encoding

问题

我已经翻译了你提供的代码段,如下所示:

  1. import json
  2. import time
  3. import hmac
  4. import hashlib
  5. import requests
  6. secret = "somekey"
  7. url = f"https://api.coindcx.com/exchange/v1/orders/status"
  8. timeStamp = int(round(time.time() * 1000))
  9. body = {
  10. "id": "ead19992-43fd-11e8-b027-bb815bcb14ed",
  11. "timestamp": timeStamp
  12. }
  13. json_body = json.dumps(body, separators=(',', ':'))
  14. signature = hmac.new(secret.encode('utf-8'), json_body.encode('utf-8'), hashlib.sha256).hexdigest()
  15. print(signature)
  16. headers = {
  17. 'Content-Type': 'application/json',
  18. 'X-AUTH-APIKEY': "someapikey",
  19. 'X-AUTH-SIGNATURE': signature
  20. }
  21. response = requests.post(url, data=json_body, headers=headers)
  22. print(response)

在Go语言中,你可以这样编写代码:

  1. import (
  2. "encoding/json"
  3. "fmt"
  4. "time"
  5. "crypto/hmac"
  6. "crypto/sha256"
  7. "encoding/hex"
  8. "net/http"
  9. "bytes"
  10. )
  11. type GetOrderStatusInput struct {
  12. ID string `json:"id"`
  13. Timestamp int64 `json:"timestamp"`
  14. }
  15. func main() {
  16. secret := "somekey"
  17. url := "https://api.coindcx.com/exchange/v1/orders/status"
  18. timeStamp := time.Now().UnixNano() / 1000000
  19. getOrderStatusInput := GetOrderStatusInput{
  20. ID: "ead19992-43fd-11e8-b027-bb815bcb14ed",
  21. Timestamp: timeStamp,
  22. }
  23. jsonBody, err := json.Marshal(getOrderStatusInput)
  24. if err != nil {
  25. fmt.Println("Error encoding JSON:", err)
  26. return
  27. }
  28. signature := generateSignature(secret, jsonBody)
  29. fmt.Println(signature)
  30. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
  31. if err != nil {
  32. fmt.Println("Error creating request:", err)
  33. return
  34. }
  35. req.Header.Set("Content-Type", "application/json")
  36. req.Header.Set("X-AUTH-APIKEY", "someapikey")
  37. req.Header.Set("X-AUTH-SIGNATURE", signature)
  38. client := &http.Client{}
  39. resp, err := client.Do(req)
  40. if err != nil {
  41. fmt.Println("Error sending request:", err)
  42. return
  43. }
  44. defer resp.Body.Close()
  45. fmt.Println(resp)
  46. }
  47. func generateSignature(secret string, data []byte) string {
  48. h := hmac.New(sha256.New, []byte(secret))
  49. h.Write(data)
  50. signature := hex.EncodeToString(h.Sum(nil))
  51. return signature
  52. }

希望这可以帮助到你!如果你有任何其他问题,请随时问我。

英文:

I have written this python code -

  1. import json
  2. import time
  3. import hmac
  4. import hashlib
  5. import requests
  6. secret = "somekey"
  7. url = f"https://api.coindcx.com/exchange/v1/orders/status"
  8. timeStamp = int(round(time.time() * 1000))
  9. body = {
  10. "id": "ead19992-43fd-11e8-b027-bb815bcb14ed",
  11. "timestamp": timeStamp
  12. }
  13. json_body = json.dumps(body, separators=(',', ':'))
  14. signature = hmac.new(secret.encode('utf-8'), json_body.encode('utf-8'), hashlib.sha256).hexdigest()
  15. print(signature)
  16. headers = {
  17. 'Content-Type': 'application/json',
  18. 'X-AUTH-APIKEY': "someapikey",
  19. 'X-AUTH-SIGNATURE': signature
  20. }
  21. response = requests.post(url, data=json_body, headers=headers)
  22. print(response)

In Go I tried writing it like this -

  1. type GetOrderStatusInput struct {
  2. ID string `json:"id"`
  3. Timestamp int64 `json:"timestamp"`
  4. }
  5. timestamp := time.Now().UnixNano() / 1000000
  6. getOrderStatusInput := GetOrderStatusInput{
  7. ID: "ead19992-43fd-11e8-b027-bb815bcb14ed",
  8. Timestamp: timestamp,
  9. }

But not able to figure out how to do json encoding and hmac here.

答案1

得分: 1

  1. package main
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/sha256"
  6. "encoding/json"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "time"
  11. )
  12. func main() {
  13. secret := "somekey"
  14. url := "https://api.coindcx.com/exchange/v1/orders/status"
  15. type GetOrderStatusInput struct {
  16. ID string `json:"id"`
  17. Timestamp int64 `json:"timestamp"`
  18. }
  19. timestamp := time.Now().UnixNano() / 1000000
  20. getOrderStatusInput := GetOrderStatusInput{
  21. ID: "ead19992-43fd-11e8-b027-bb815bcb14ed",
  22. Timestamp: timestamp,
  23. }
  24. jsonBytes, err := json.Marshal(getOrderStatusInput)
  25. if err != nil {
  26. // 处理错误
  27. }
  28. mac := hmac.New(sha256.New, []byte(secret))
  29. mac.Write(jsonBytes)
  30. signature := mac.Sum(nil)
  31. jsonBody := bytes.NewReader(jsonBytes)
  32. req, _ := http.NewRequest("POST", url, jsonBody)
  33. req.Header.Set("Content-Type", "application/json")
  34. req.Header.Set("X-AUTH-APIKEY", "someapikey")
  35. req.Header.Set("X-AUTH-SIGNATURE", string(signature))
  36. client := &http.Client{}
  37. resp, err := client.Do(req)
  38. if err != nil {
  39. // 处理错误
  40. }
  41. defer resp.Body.Close()
  42. body, err := ioutil.ReadAll(resp.Body)
  43. if err != nil {
  44. // 处理错误
  45. }
  46. fmt.Println(string(body))
  47. }

以上是给定的Go语言代码的翻译。

英文:
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/sha256"
  6. "encoding/json"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "time"
  11. )
  12. func main() {
  13. secret := "somekey"
  14. url := "https://api.coindcx.com/exchange/v1/orders/status"
  15. type GetOrderStatusInput struct {
  16. ID string `json:"id"`
  17. Timestamp int64 `json:"timestamp"`
  18. }
  19. timestamp := time.Now().UnixNano() / 1000000
  20. getOrderStatusInput := GetOrderStatusInput{
  21. ID: "ead19992-43fd-11e8-b027-bb815bcb14ed",
  22. Timestamp: timestamp,
  23. }
  24. jsonBytes, err := json.Marshal(getOrderStatusInput)
  25. if err != nil {
  26. // handle error
  27. }
  28. mac := hmac.New(sha256.New, []byte(secret))
  29. mac.Write(jsonBytes)
  30. signature := mac.Sum(nil)
  31. jsonBody := bytes.NewReader(jsonBytes)
  32. req, _ := http.NewRequest("POST", url, jsonBody)
  33. req.Header.Set("Content-Type", "application/json")
  34. req.Header.Set("X-AUTH-APIKEY", "someapikey")
  35. req.Header.Set("X-AUTH-SIGNATURE", string(signature))
  36. client := &http.Client{}
  37. resp, err := client.Do(req)
  38. if err != nil {
  39. // handle error
  40. }
  41. defer resp.Body.Close()
  42. body, err := ioutil.ReadAll(resp.Body)
  43. if err != nil {
  44. // handle error
  45. }
  46. fmt.Println(string(body))
  47. }

huangapple
  • 本文由 发表于 2021年8月12日 14:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/68752838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定