-3

Following on from the following question

WPF / C# Submit button to POST API

I now want to amend the POST BODY to include Username and Password Variable.

The below is what I am using at the moment

var OktaUserName = ADAccountName;
var OktaPassword = Password;

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://trial.okta.com/api/v1/authn");

request.Content = new StringContent("{\"username\":"+OktaUserName+",\"password\":"+OktaPassword+",\"options\": {\"multiOptionalFactorEnroll\": true,\"warnBeforePasswordExpired\": true}}", Encoding.UTF8, "application/json");

var response = await client.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
MessageBox.Show("Alert", json, "OK");
MessageBox.Show(responseContent, "Message");

I expect that the username and password is pulled from the stored variables and then added to OKTA as a new user, however with the above I get an error

ErrorCode E000003 Error Summary The Request Body was not well-formed

Any help will be greatly appreciated

Fildor
  • 14,510
  • 4
  • 35
  • 67
Foadi
  • 11
  • 3
  • Your content string values are missing " – Fildor Nov 15 '22 at 14:15
  • If you mean the ADAccountName and Password strings, they are used in the code above (not shown). Even if I replace the variables with a text string such as var OktaUserName = "Name"; var OktaPassword = "password123"; I still get the same error – Foadi Nov 15 '22 at 14:52
  • The are missing a pair of " each. Take a look at the string as it goes into the content. – Fildor Nov 15 '22 at 16:06

1 Answers1

0

Thank you @Fildor, your answers helped.

My modified request.Content is as follows

request.Content = new StringContent("{\"username\":\""+ADAccountNameStr+"\",\"password\":\""+PasswordStr+"\",\"options\": {\"multiOptionalFactorEnroll\": true,\"warnBeforePasswordExpired\": true}}", Encoding.UTF8, "application/json");
Foadi
  • 11
  • 3