
f4c3l (f4c3l) asked a question.
I am working with OKTA User API and trying to Create/Update user from .NET Core service. But i am getting "The request body was not well-formed". The below code i used to make API call to OKTA.
public static class ApiHelper
{
public static void AddHeaders(HttpRequestHeaders headers, string authType = "", string secret = "")
{
headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (!string.IsNullOrWhiteSpace(authType) && !string.IsNullOrWhiteSpace(secret))
{
headers.Authorization = new AuthenticationHeaderValue(authType, secret);
}
}
public static async Task<T> GetAsync<T>(string url, string authType = "", string secret = "")
{
using (var client = new HttpClient())
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(url)
};
AddHeaders(request.Headers, authType, secret);
var response = await client.SendAsync(request);
var result = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<T>(result);
}
}
public static async Task<TResult> PostAsync<TResult, TResponse>(string url, TResponse data, string authType = "", string secret = "")
{
using (var client = new HttpClient())
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(url)
};
AddHeaders(request.Headers, authType, secret);
request.Content = new JsonContent(data);
var response = await client.SendAsync(request);
var result = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<TResult>(result);
}
}
}
public class JsonContent : StringContent
{
public JsonContent(object payload) :
base( JsonConvert.SerializeObject(payload), Encoding.Unicode, "application/json")
{
}
}
Note: Get data from OKTA is working, but POST data to OKTA is not working.
POST URL - https://<companyapp>.oktapreview.com/api/v1/users?activate=true
JSON: {
"id": null,
"status": null,
"profile": {
"firstName": "demo",
"lastName": "user",
"email": "demo.user@company.com",
"login": "demo.user@company.com",
"department": "Art",
"mobilePhone": "1111111111"
}
}
I tried in POSTMAN the same data is working. It is not working in .NET Core. Please look into this and provide a fix/workaround asap.

Hello Theresa,
Thank you for posting in the community forum. Looking at the data being passed to the Okta itself which has required the First name, Last name, username and email. Kindly open a case with Okta support and we can have one of our developer support Engineer to assist you with the .net portion of the code and provide assistance.
Thank you,
Ashwin