i am using an API which named PSPDFKIT, this API can convert your local field in .pdf field .It has already write many language to demonstration,but i have a few proplem when converting in to golang. This is the version of Python which provided by PSPDFKIT: `
import requests
import json
instructions = {
'parts': [
{
'html': 'index.html'
}
]
}
response = requests.request(
'POST',
'https://api.pspdfkit.com/build',
headers = {
'Authorization': 'Bearer API key'
},
files = {
'index.html': open('index.html', 'rb')
},
data = {
'instructions': json.dumps(instructions)
},
stream = True
)
if response.ok:
with open('result.pdf', 'wb') as fd:
for chunk in response.iter_content(chunk_size=8096):
fd.write(chunk)
else:
print(response.text)
exit()
`
and this is my code:
`
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
type instructions struct {
Parts []struct {
HTML string `json:"html"`
} `json:"parts"`
}
func main() {
instructions := `{
'parts': [
{
'html': 'index.html'
}
]
}`
instructionsBytes, err := json.Marshal(instructions)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
file, err := os.Open("document.pdf")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
response, _ := http.NewRequest("POST", "https://api.pspdfkit.com/build", bytes.NewBuffer(instructionsBytes))
defer response.Body.Close()
response.Header.Set("Authorization", "API key")
response.Header.Set("Content-Type", "multipart/form-data")
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(body))
os.Exit(1)
result, err := os.Create("result.pdf")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer result.Close()
_, err = io.Copy(result, response.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
` This API request the multipart/form-data in json:instructions and local field i guess,but i am not sure how to post multipart/form-data correctly,and i always received the empty .pdf field after running my code.
Can anyone help me? thanks a lot .
i wish to know how to post multipart/form-data correctly,also the right way to convert the code in golang,and finally it can produce the .pdf field that are not empty PDF.