curl 工作但是 PostAsync returns 500 内部服务器错误
curl works however PostAsync returns 500 internal server error
我正在尝试将以下代码转换为 c#.net
curl -X 'POST' \
'https://someapi.io/api/function1' \
-H 'accept: application/json' \
-H 'X-API-Key: key' \
-H 'Content-Type: application/json' \
-d '{
"abi": [{"inputs":[]}],
"params": { "userAddress": "address" }
}'
但是我收到 500 内部服务器错误,而上面的代码工作正常。
var obj = new { abi = "[{\"inputs\":[]}]", @params = new { userAddress = "address" } };
var payload = JsonContent.Create(json);
// StringContent payload = new StringContent(JsonSerializer.Serialize(json), Encoding.UTF8, "application/json");
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Add("X-API-Key", "key");
using (HttpResponseMessage response = await _client.PostAsJsonAsync($"{baseUri}?{signature}", payload).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
}
有什么想法是错误的吗?
abi
需要作为数组传递,但您将其作为字符串传递:
var obj = new { abi = new[]
{
new {
inputs = new string[0] //replace string with the array type you want
}
},
@params = new { userAddress = "address" } };
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Add("X-API-Key", "key");
using (HttpResponseMessage response = await _client.PostAsJsonAsync($"{baseUri}?{signature}", obj).ConfigureAwait(false)) //pass obj here
{
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
}
我正在尝试将以下代码转换为 c#.net
curl -X 'POST' \
'https://someapi.io/api/function1' \
-H 'accept: application/json' \
-H 'X-API-Key: key' \
-H 'Content-Type: application/json' \
-d '{
"abi": [{"inputs":[]}],
"params": { "userAddress": "address" }
}'
但是我收到 500 内部服务器错误,而上面的代码工作正常。
var obj = new { abi = "[{\"inputs\":[]}]", @params = new { userAddress = "address" } };
var payload = JsonContent.Create(json);
// StringContent payload = new StringContent(JsonSerializer.Serialize(json), Encoding.UTF8, "application/json");
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Add("X-API-Key", "key");
using (HttpResponseMessage response = await _client.PostAsJsonAsync($"{baseUri}?{signature}", payload).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
}
有什么想法是错误的吗?
abi
需要作为数组传递,但您将其作为字符串传递:
var obj = new { abi = new[]
{
new {
inputs = new string[0] //replace string with the array type you want
}
},
@params = new { userAddress = "address" } };
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Add("X-API-Key", "key");
using (HttpResponseMessage response = await _client.PostAsJsonAsync($"{baseUri}?{signature}", obj).ConfigureAwait(false)) //pass obj here
{
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
}