我收到以下错误。下面用golang编写的代码有什么问题?知道吗?
&{400错误请求 400 HTTP/1.1 1 1 map[内容类型:[text/html; charset=us-ascii] 日期:[2018 年 8 月 15 日星期三 16:14:34 GMT] 内容长度:[311]] 0xc42005c280 311 [] 真假地图[] 0xc4200fe000 0xc4200a62c0}
url := fmt.Sprintf("https://api.labs.cognitive.microsoft.com/academic/v1.0/interpret?query=a two level microprogram simulator&complete=0&count=10&model=latest")
虽然 Go 代码有问题(my-key
看起来特别奇怪(,但问题是您需要转义查询参数中的空格:
$ curl 'https://api.labs.cognitive.microsoft.com/academic/v1.0/interpret?query=a two level microprogram simulator&complete=0&count=10&model=latest'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request</h2>
<hr><p>HTTP Error 400. The request is badly formed.</p>
</BODY></HTML>
使用%20
转义空格后,我们得到预期的访问错误:
$ curl 'https://api.labs.cognitive.microsoft.com/academic/v1.0/interpret?query=a%20two%20level%20microprogram%20simulator&complete=0&count=10&model=latest'
{"error":{"code":"Unspecified","message":"Access denied due to invalid subscription key. Make sure you are subscribed to an API you are trying to call and provide the right key."}}
最好让Go处理这个:
import "net/url"
base_url := "https://api.labs.cognitive.microsoft.com/academic/v1.0/interpret"
var v url.Values
v.Add("query", "a two level microprogram simulator")
v.Add("complete", "0")
v.Add("count", "10")
v.Add("model", "latest")
url := base_url + "?" + v.Encode()
其实早些时候我也想通了。所以我的建议是简单地添加 fol 网址。QueryEscape("一个两级微程序模拟器"(