为什么这个Go/GraphQL查询返回HTTP 415错误?
我尝试按照的Go/GraphQL方法连接到特定的API。以下是Python/Jupyter中的工作:为什么这个Go/GraphQL查询返回HTTP 415错误?,go,graphql,Go,Graphql,我尝试按照的Go/GraphQL方法连接到特定的API。以下是Python/Jupyter中的工作: import requests import json query = """query lineSearch { lines(transportModes: water) { id quays { name id latitude longitude } transportSub
import requests
import json
query = """query lineSearch {
lines(transportModes: water) {
id
quays {
name
id
latitude
longitude
}
transportSubmode
}
}"""
url = 'https://api.entur.io/journey-planner/v2/graphql'
r = requests.post(url, json={'query': query})
print(r.text)
下面的Go尝试返回Goland IDE中的HTTP 415不支持的媒体类型
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
func main() {
jsonData := map[string]string{
"query": `query {lines(transportModes: water) {id quays {name id latitude longitude} transportSubmode }}`,
}
jsonValue, _ := json.Marshal(jsonData)
request, err := http.NewRequest("POST", "https://api.entur.io/journey-planner/v2/graphql",
bytes.NewBuffer(jsonValue))
client := &http.Client{Timeout: time.Second * 10}
response, err := client.Do(request)
defer response.Body.Close()
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
}
data, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(data))
}
我是否犯了任何明显的Python to Go翻译错误?您可能缺少标题
内容类型:application/json
加上
request.Header.Add("Content-Type", "application/json")
在请求创建下方您可能缺少标题
内容类型:application/json
加上
request.Header.Add("Content-Type", "application/json")
以下是创建请求