有谁可以告诉我为什么PostAsync
的{{1}}没有发布表单数据?
在此客户发布时,我在服务器端没有数据。上次检查时,HttpClient
包含键值对中的数据,但无法在formFields
中找到数据。
formContent
答案 0 :(得分:0)
我依旧记得有类似的问题。我们最终没有使用" PostAsync"方法,但使用以下。这是我们用于测试WebAPI的辅助方法。希望这会有所帮助。
public async Task<TestHttpResponseMessage> Post(string url, string jsonPayload = "", Dictionary<string, string> headers = null, IFilter filter = null, HttpContent content = null)
{
using (var server = CreateTestserver(filter))
{
if (!url.StartsWith("http"))
url = "http://localhost/" + url;
var request = new HttpRequestMessage
{
RequestUri = new Uri(url),
Method = HttpMethod.Post,
Content = content ?? new StringContent(jsonPayload, Encoding.UTF8, "application/json")
};
request.Headers.Add("usertoken", OpenApiApplication.TestApiKey.ToString());
if (headers == null)
headers = new Dictionary<string, string>();
foreach (var header in headers)
request.Content.Headers.Add(header.Key, header.Value);
var httpResponseMessage = await server.HttpClient.SendAsync(request);
var stringResult = await httpResponseMessage.Content.ReadAsStringAsync();
return new TestHttpResponseMessage { JsonObject = Json.Decode(stringResult), HttpResponseMessage = httpResponseMessage };
}
}