softlayer / softlayer-go

SoftLayer API Client for the Go Language
Apache License 2.0
53 stars 46 forks source link

GetPdf() type mismatch - can't unmarshal uint8 to string #152

Open viewpnt opened 4 years ago

viewpnt commented 4 years ago

Hi: I'm trying to save a pdf or excel invoice, but I get an error after the call to getPdf() or getExcel() - reading body error: type mismatch - can't unmarshal uint8 to string

With sess.Debug = true I can see the base64 encoded pdf but I'm unable to save the result to a file.

Is there a bug with this methods? or I'm doing something wrong?

package main

import (
    "fmt"
    "io/ioutil"
    "github.com/softlayer/softlayer-go/services"
    "github.com/softlayer/softlayer-go/session"
)

func main() {
    sess := session.New()

    sess.Debug = true

    invoiceService := services.GetBillingInvoiceService(sess)

    pdf, err := invoiceService.Id(9359539).GetPdf()

    if err != nil {
        fmt.Printf("\n Unable to retrieve invoice's Pdf:\n - %s\n", err)
        return
    }

    ioutil.WriteFile("/tmp/invoice.pdf", pdf, 0644)

}
allmightyspiff commented 4 years ago

Looks like this is a problem with the XMLRPC parser.

For now, try using the REST endpoint ( https://api.softlayer.com/rest/v3.1/ in your ~/.softlayer file)

the only issue with doing that is the response will be literally "<BASE64STRING>" so you'll have to remove the quotes around the string, and then base64 decode it to use as a pdf.

(py36) chris@SPF-WIN10:~/go/src/github.com/softlayer/softlayer-go$ cat testIssues152.go
package main

import (
        "fmt"
        "io/ioutil"
        "github.com/softlayer/softlayer-go/services"
        "github.com/softlayer/softlayer-go/session"
)

func main() {
        sess := session.New()
        sess.Debug = true
        invoiceService := services.GetBillingInvoiceService(sess)
        pdf, err := invoiceService.Id(54668752).GetPdf()
        if err != nil {
                fmt.Printf("\n Unable to retrieve invoice's Pdf:\n - %s\n", err)
                return
        }
        ioutil.WriteFile("./invoice.pdf", pdf, 0644)
}

REST

(py36) chris@SPF-WIN10:~/go/src/github.com/softlayer/softlayer-go$ ./testIssues152
2020/05/28 10:39:54 [DEBUG] Request URL:  GET https://api.softlayer.com/rest/v3.1/SoftLayer_Billing_Invoice/54668752/getPdf.json
2020/05/28 10:39:54 [DEBUG] Parameters:
2020/05/28 10:39:55 [DEBUG] Status Code:  200
2020/05/28 10:39:55 [DEBUG] Response:  "JVBERi0xLjcKJeLjzwMF0gL0JsZWVkQm94IFswLjAwMDAwMCAwLjAwMDAwMCA1OTUuMjc2MDAwIDg0MS44OTAwMDBdIC9UcmltQm94IFswLjAwMDAwMCAwLjAwMDAwMCA1OTUuMjc2MDAwIDg0MS44OTAwMDBdIC9.........OMMITTED

(py36) chris@SPF-WIN10:~/go/src/github.com/softlayer/softlayer-go$ cat invoice.pdf
"JVBERi0xLjcKJeLjz9MKMTAgMCBvYHozi+ka9TYfYOPqNuunDZvBProukrPLptSrgNE7TvFE9alTRRh2Ma76\/ZqWTfJs4g3xiESpvo17ko+rv+EbH9G1WqSKLU+hBX\/1UaT6dqKyyiJyh9pBPJrMsqW7sQZRRG....OMMITTED

XMLRPC

$ ./testIssues152.exe
2020/05/28 10:42:50 ->>>Request:
2020/05/28 10:42:50 POST /xmlrpc/v3.1/SoftLayer_Billing_Invoice HTTP/1.1
Host: api.softlayer.com
User-Agent: Go-http-client/1.1
Content-Length: 800
Content-Type: text/xml
Accept-Encoding: gzip

<?xml version="1.0" encoding="UTF-8"?><methodCall><methodName>getPdf</methodName><params><param><value><struct><member><name>headers</name><value><struct><member><name>User-Agent</name><value><string>softlayer-go/v0.1.0-alpha (go1.14.1;amd64;windows)</string></value></member><member><name>authenticate</name><value><struct><member><name>username</name><value><string>XXXXX</string></value></member><member><name>apiKey</name><value><string>XXXXXXXXXXXXXXXXXX</string></value></member></struct></value></member><member><name>SoftLayer_Billing_InvoiceInitParameters</name><value><struct><member><name>id</name><value><int>54668752</int></value></member></struct></value></member></struct></value></member></struct></value></param></params></methodCall>
2020/05/28 10:42:50

<<<-Response:
2020/05/28 10:42:50 HTTP/1.1 200 OK
Connection: close
Transfer-Encoding: chunked
Content-Type: text/xml;charset=UTF-8
Date: Thu, 28 May 2020 15:42:49 GMT
Server: Apache
Strict-Transport-Security: max-age=31536000
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN

fd8b
<?xml version="1.0" encoding="utf-8"?>
<params>
<param>
 <value>
  <base64>JVBERi0xL.........OMMITTED10;</base64>
 </value>
</param>
</params>

0

 Unable to retrieve invoice's Pdf:
 - reading body error: type mismatch - can't unmarshal uint8 to string
viewpnt commented 4 years ago

Hi:

Thank you for the workaround, now I'm able to save the pdf file after base64 decode it.