on 2024 Sep 02 1:07 PM
Hi there,
I am trying to connect to the RESTful api using c#, but having some difficulties getting the syntax correct.
I'm currently receiving a 401 response error. I am an administrator and the RESTful base url is setup.
I have the required response body:
<attrs xmlns="http://www.sap.com/rws/bip">
<attr name="password" type="string"/>
<attr name="clientType" type="string"/>
<attr name="auth" type="string" possibilities="secEnterprise,secLDAP,secWinAD,secSAPR3">secEnterprise</attr>
<attr name="userName" type="string"/>
</attrs>The C# code I'm using is:
public async Task<string> AuthenticateAsync()
{
using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
{
// Construct the XML request body
var requestBody = $@"
<attrs xmlns=""http://www.sap.com/rws/bip"">
<attr name=""password"" type=""string"">{password}</attr>
<attr name=""clientType"" type=""string"">SDK</attr>
<attr name=""auth"" type=""string"" possibilities=""secEnterprise,secLDAP,secWinAD,secSAPR3"">secWinAD</attr>
<attr name=""userName"" type=""string"">{username}</attr>
</attrs>";
StringContent content = new StringContent(requestBody, System.Text.Encoding.UTF8, "application/xml");
var response = await client.PostAsync($"{baseUrl}/logon/long", content);
GD.Print("response", response);
if (response.IsSuccessStatusCode)
{
var responseData = await response.Content.ReadAsStringAsync();
var xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(responseData);
// Assuming the token is in a specific XML element
var tokenNode = xmlDoc.SelectSingleNode("//attr[@name='LogonToken']");
if (tokenNode != null)
{
string logonToken = tokenNode.InnerText.Trim();
return logonToken;
}
else
{
throw new Exception("Token not found in the response.");
}
}
else
{
throw new Exception("Authentication failed: " + response.ReasonPhrase);
}
}
}At this stage I comment out what's in the if statement as I am only interested in the response.
I have tried it without the clientType as I am not entirely sure what that needs to be.
I was hoping someone could see something I've missed.
Thanks,
Paul
Request clarification before answering.
what base URL are you using? it should be http(s)://<server>:<RESTful port>/biprws
You also missed the header "Accept" application/xml
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi ayman_salem,
Thanks for reply.
The baseurl is in the form of http://<server>:6405/biprws.
I had added this syntax previously but didn't see a change:
using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
{
// Add the Accept header to the request
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
// code continued here...This is the reponses I get both when hardcoding the credentials and baseurl and with variables :
responseStatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Server: Apache-Coyote/1.1
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://<servername>:8080
Access-Control-Expose-Headers: X-SAP-LogonToken, X-SAP-PVL, WWW-Authenticate
Date: Tue, 03 Sep 2024 05:22:23 GMT
Content-Type: application/xml
Content-Length: 431
}or
responseStatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Server: Apache-Coyote/1.1
Date: Tue, 03 Sep 2024 05:30:13 GMT
Content-Type: application/xml
Content-Length: 431
}The url to get the response works so assume the api is accepting requests
http://servername:6405/biprws/logon/longIs there a log file I can check for more debug information. I wonder if the requestBody syntax is not correct.
| User | Count |
|---|---|
| 7 | |
| 5 | |
| 5 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.