如何在GoLang中处理内容类型azip和内容编码application / a-gzip?

时间:2018-11-15 09:49:47

标签: go curl httprequest appstoreconnect

我正在尝试使用App Store Connect API通过GoLang下载财务和销售报告。

我成功将呼叫连接到App Store API服务器并收到正确的输出。

由于我无法下载该报告,因为它们正在以内容类型“ azip”发送以字节为单位的响应。

下面是我的Go代码:

How many days are you going to spend travelling? 14
How much money, in USD, are you planning to spend on your trip? 2300
What is the three letter currency symbol for your travel destination? MXC
How many MXC are there in 1 USD? 19.8
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
    at TripPlanner.travelTimeAndBudget(TripPlanner.java:33)
    at TripPlanner.main(TripPlanner.java:8)

下面是我得到的标题。

url := "https://api.appstoreconnect.apple.com/v1/salesReports?filter[frequency]=DAILY&filter[reportType]=SUBSCRIPTION_EVENT&filter[reportSubType]=SUMMARY&filter[vendorNumber]=87389901&filter[reportDate]=2018-10-12&filter[version]=1_1"

req, _ := http.NewRequest("GET", url, nil)

req.Header.Add("Authorization", "Bearer eyJhbGciOiJFUzI1NiIsImtpZCI6IjZROUM1WVc0N1QifQ.eyJpc3MiOiI2OWE2ZGU5MC00NGNiLTQ3ZTMtZTA1My01YjhjN2MxMWE0ZDEiLCJleHAiOjE1NDIyNzM0MzcsImF1ZCI6ImFwcHN0b3JlY29ubmVjdC12MSJ9.fC4zmgaUZZTwtopt9gaSl6HUtACNjBvWi5VNa366d5FI5l_00wM1U2cBi26a0jtB8yuZOufDSQA0MB5iMC332g")

res, _ := http.DefaultClient.Do(req)

fmt.Println(res.Header.Get("Content-Encoding"))

defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)

fmt.Println(res)
fmt.Println(string(body))

我可能做错了什么。但是请告诉我如何使用GoLang将其存储为文件。

1 个答案:

答案 0 :(得分:0)

您需要在标头中添加带有“ application / a-gzip”值的参数Accept

示例:

request, err := http.NewRequest("GET", "https://api.appstoreconnect.apple.com/v1/salesReports", nil)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", token.Bearer))
request.Header.Add("Accept", "application/a-gzip")