Я использую эту конечную точку в PostMan: -
https://api.dialogflow.com/v1/query?v=20150910
И эту строку JSON я отправляю: -
{"query":"flights from NYC to Vadodara","sessionId":"6b9d4676-2a71-4c64-a562-8c08f198c623","lang":"pt-BR","resetContexts":false}
И я устанавливаю содержимое-тип и авторизация в почтальоне.
Все эти вещи прекрасно работают в почтальоне, но проблема в том, что когда я нажимаю на эту конечную точку, используя код c #, он не работает, выдает мне эту ошибку: -
ОшибкаЯ получаю: -
{
"id": "7b0ac743-58ea-4d61-a41d-a299f086a816",
"timestamp": "2018-06-04T15:30:25.873Z",
"lang": "en",
"status": {
"code": 400,
"errorType": "bad_request",
"errorDetails": "Invalid request content type, expecting \"multipart/form-data\" or \"application/json; charset\u003dutf-8."
}
}
Вот мой код:
using ApiAi.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
HttpClient http = new HttpClient();
http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
http.DefaultRequestHeaders.Add("ContentType", "application/json; charset=utf-8");
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxx");
var response = http.PostAsync("https://api.dialogflow.com/v1/query?v=20150910", new StringContent(new Program().getSessionID(new ConfigModel { AccesTokenClient = "xxx" }, "flights from NYC to Vadodara"))).Result.Content.ReadAsStringAsync().Result;
}
public string getSessionID(ConfigModel config, string message)
{
var requestData = new RequestModel
{
query = message,
sessionId = (config.SessionId ?? Guid.NewGuid()).ToString(),
lang = "pt-BR"
};
return JsonConvert.SerializeObject(requestData);
}
}
public class RequestModel
{
public string query { get; set; }
public string sessionId { get; set; }
public string lang { get; set; }
public bool resetContexts { get; set; }
}
}
//
// Copyright (c) 2017 Nick Rimmer. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
using ApiAi.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApiAi.Models
{
/// <summary>
/// Services configuration
/// </summary>
public class ConfigModel
{
#region magic
internal static string
BaseUrl = @"https://api.dialogflow.com/v1",
VersionCode = @"20150910";
#endregion
/// <summary>
/// Each API request requires authentication to identify the agent that is responsible for making the request. Authentication is provided through an access token.
/// The developer access token is used for managing entities and intents.
/// </summary>
public string AccesTokenDeveloper { get; set; }
/// <summary>
/// Each API request requires authentication to identify the agent that is responsible for making the request. Authentication is provided through an access token.
/// The client access token is used for making queries.
/// </summary>
public string AccesTokenClient { get; set; }
/// <summary>
/// Specifed language in your Api.ai agent
/// </summary>
public LanguagesEnum Language { get; set; }
/// <summary>
/// Timezone requests parameter
/// </summary>
public string TimeZone { get; set; } = System.TimeZone.CurrentTimeZone.StandardName;
/// <summary>
/// Session ID for request
/// </summary>
public object SessionId { get; set; } = Guid.NewGuid();
}
}
Это код, который я использую.Заранее спасибо.