公式
ソース
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<span class="k">パッケージ</span> <span class="n">メイン</span> <span class="k">インポート</span> <span class="p">(</span> <span class="s">"bytes" </span> <span class="s">"fmt" </span> <span class="s">"io / ioutil" </span> <span class="s">"net / http" </span> <span class="s">"strconv" </span> <span class="p">)</span> <span class="k">func </span> <span class="n">main </span><span class="p">()</span> <span class="p">{ </span> <span class="c">// </span> <span class="c">QnA Maker操作の// </span> <span class="c">HTTPリクエストURIの作成に使用されるさまざまな要素</span><span class="c">を表します。</span><span class="c">//公開ページから:HOST </span> <span class="c">//例:https://YOUR-RESOURCE-NAME.azurewebsites.net/qnamaker </span> <span class="k">var </span> <span class="n">host </span> <span class="kt">string </span> <span class="o">= </span> <span class="s">"https://qa-bot-service.azurewebsites.net /qnamaker"</span> <span class="c">//承認エンドポイントキー</span> <span class="c">//公開ページから</span> <span class="k">var </span> <span class="n">endpoint_key </span> <span class="kt">string </span> <span class="o">= </span> <span class="s">"ffdbcf11-cxxxxxxxxxxxxxxxxxxxxxxxa"</span> <span class="c">//管理APIがバージョンをルートに延期する</span> <span class="c">//公開ページから、POST後の値</span> <span class="c">//例:/ knowledgebases / xxxxxxxxxxxxxxx / generateAnswer </span> <span class="k">var </span> <span class="n">route </span> <span class="kt">string </span> <span class="o">= </span> <span class="s">"/ knowledgebases / xxxxxxxxxxxxxxxxxxxx / generateAnswer"</span> <span class="c">//サービスに質問を渡すためのJSON形式</span> <span class="k">var </span> <span class="n">question </span> <span class="kt">string </span> <span class="o">= </span> <span class="s">"{'question': 'パスワード'、 'top':3}"</span> <span class="n">req </span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">http </span><span class="o">。</span><span class="n">NewRequest </span><span class="p">(</span><span class="s">"POST" </span><span class="p">、</span> <span class="n">ホスト</span><span class="o">+ </span><span class="n">ルートは</span><span class="p">、</span> <span class="n">バイト</span><span class="o">。 </span><span class="n">NewBuffer </span><span class="p">([] </span><span class="kt">バイト</span><span class="p">(</span><span class="n">質問</span><span class="p">)))</span> <span class="n">REQを</span><span class="o">。</span><span class="n">ヘッダー</span><span class="o">。</span><span class="p">(</span><span class="s">"Authorization" </span><span class="p">、</span><span class="s">"EndpointKey" </span><span class="o">+ </span><span class="n">endpoint_key </span><span class="p">)</span><span class="n">req </span><span class="n">を 追加し</span><span class="n">ます</span><span class="o">。</span><span class="n">ヘッダー</span><span class="o">。</span><span class="p">(</span><span class="s">"Content-Type" </span><span class="p">、</span><span class="s">"application / json" </span><span class="p">)</span><span class="n">req </span><span class="n">を追加し</span><span class="s">ます</span><span class="o">。 </span><span class="n">ヘッダー</span><span class="o">。</span><span class="n">追加</span><span class="p">(</span> <span class="s">"Content-Length" </span><span class="p">、</span> <span class="n">strconv </span><span class="o">。</span><span class="n">Itoa </span><span class="p">(</span><span class="nb">len </span><span class="p">(</span><span class="n">質問</span><span class="p">)))</span> <span class="n">クライアント</span> <span class="o">:= </span> <span class="o">&</span><span class="n">http </span><span class="o">。</span><span class="n">クライアント</span><span class="p">{} </span> <span class="n">応答</span><span class="p">、</span> <span class="n">エラー</span> <span class="o">:= </span> <span class="n">クライアント</span><span class="o">。</span><span class="n">err </span><span class="o">!= </span><span class="no">nil </span><span class="p">{ </span><span class="nb">panic </span><span class="p">(</span><span class="n">err </span><span class="p">)</span><span class="p">}の</span><span class="k">場合</span><span class="n">は</span><span class="p">(</span><span class="n">req </span><span class="p">)を実行</span> <span class="n">応答を</span><span class="k">延期する</span><span class="o">。</span><span class="n">ボディ</span><span class="o">。</span><span class="n">close </span><span class="p">()</span> <span class="n">body </span><span class="p">、</span> <span class="n">_ </span> <span class="o">:= </span> <span class="n">ioutil </span><span class="o">。</span><span class="n">ReadAll </span><span class="p">(</span><span class="n">応答</span><span class="o">。</span><span class="n">本文</span><span class="p">)</span> <span class="n">fmt </span><span class="o">。</span><span class="n">Printf </span><span class="p">(</span><span class="kt">string </span><span class="p">(</span><span class="n">body </span><span class="p">)</span> <span class="o">+ </span> <span class="s">" </span><span class="se">\ n </span><span class="s">" </span><span class="p">)</span> <span class="p">}</span> |
実行
1 |
main.goを実行します |