public static string SendRequest(HttpMethod method, string url, object data)
{
try
{
// Initialize HttpClient
using HttpClient httpClient = new HttpClient();
var (username, password, headers) = ParseUrl(url);
// Set up basic authorization header
string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
JsonSerializerOptions options = new JsonSerializerOptions() { DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull };
// Serialize data object to JSON
//string jsonData = JsonSerializer.Serialize(data, options);
string jsonData = "{\"ds\":{\"PartDim\":[],\"PartPC\":[],\"EntityGLC\":[],\"Part\":[],\"PartAttch\":[],\"PartAudit\":[],\"PartBinInfo\":[],\"PartCOO\":[],\"PartCOPart\":[],\"PartLangDesc\":[],\"PartPlanningPool\":[],\"PartPlant\":[],\"PartPlantPlanningAttribute\":[],\"PartRestriction\":[],\"PartRestrictSubst\":[],\"PartRev\":[],\"PartRevAttch\":[],\"PartRevInspPlan\":[],\"PartRevInspPlanAttch\":[],\"PartRevInspVend\":[],\"PartSubs\":[],\"PartUOM\":[],\"PartWhse\":[],\"TaxExempt\":[]}}\r\n";
// Create StringContent with JSON data
StringContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
if (headers != null)
{
foreach (KeyValuePair<string, string> header in headers)
{
content.Headers.Add(header.Key, header.Value);
}
}
// Create HttpRequestMessage with specified method and URL
HttpRequestMessage request = new HttpRequestMessage(method, url) { Content = content };
// Send the request
HttpResponseMessage response = httpClient.Send(request);
// Handle response as needed
if (response.IsSuccessStatusCode)
{
string responseBody = response.Content.ReadAsStringAsync().Result;
return responseBody;
}
else
{
return $"{method} Request failed. Status code: {response.StatusCode}";
}
}
catch (HttpRequestException e)
{
return $"Request failed: {e.Message}";
}
}
I’m attempting a POST at this endpoint “/server/api/v1/Erp.BO.PartSvc/GetNewPart”. This method should return a part with default values. In the code above the responseBody = “”, with a StatusCode = 200.
Why is the responseBody a blank string instead of the expected default part?