02-28-2019 01:37 AM
Hello,
i wanted to develop a little tool to change the user state using c# and the finesse rest api.
Problem is, when i execute the method shown below i get an http response saying "Bad Request".
Requesting the Userinformation using GET works fine.
-------------------------------------------------------------------------------------------
Client = new RestClient("https://b001cvs000621.bechtle.de:8445/");
Request = new RestRequest();
Request.Resource = "finesse/api/User/" + username;
Request.Credentials = new NetworkCredential(username,password);
private void ChangeState() {
String state= GetState();
if (state== "NOT_READY") {
XElement XML = new XElement("User",new XElement("state","READY"));
Request.AddXmlBody(XML);
var response = Client.Execute(Request,Method.PUT);
MessageBox.Show(response.StatusDescription);
}
else {
XElement XML = new XElement("User",new XElement("state","NOT_READY"));
Request.AddXmlBody(XML);
var response = Client.Execute(Request,Method.PUT);
MessageBox.Show(response.StatusDescription);
}
}
-------------------------------------------------------------------------------------------
GetState() simply calls an http get request and returns the Userstate as a string
I am new to this forum so feel free to put this discussion into the right location.
Thanks,
Julian
02-28-2019 10:29 AM
Hi,
In your request, are you adding the header "Content-Type" with the value of "application/xml"? I don't see it in the snippet of code, so wasn't 100% sure.
Also, if you are getting a 400 Bad request, there should be an <ApiError> in the response. What does that say?
Thanx,
Denise
03-01-2019 01:17 AM
The ApiError says:
<ErrorType>Invalid Input</ErrorType>
<ErrorData>User</User>
<ErrorMessage>XElement : XElement</ErrorMessage>
I tried adding the Content-Type but if i do so my GetState() returns an error page:
HTTP Status 415 - Unsupported Media Type
Type: Status report
message: Unsupported Media Type
Description: The server refused this request because this request entitiy is in a format not supported by the requested resource for the requested method
____________________________________________________________________________________
Client.AddDefaultHeader("Content-Type","application/xml");
private String GetState()
{
Request.Method = Method.GET;
byte[] response = Client.DownloadData(Request);
String text = "";
foreach (Char character in response)
text = text + character;
String state= text.Substring(text.IndexOf("<state>") + 7,text.IndexOf("</state>") - ( text.IndexOf("<state>") + 7 ));
return state;
}
03-01-2019 09:23 AM
Hi,
The Content-Type only applies for PUT/POST and not a GET. So that is an expected error if you added it for the GET.
Based on the ApiError you are getting, it seems like the body of the change agent state request isn't right. It appears that it isn't even seeing the <User> tag. I haven't used C# in a really long time, so I've forgotten how to build XML programmatically. The request is expecting the body to be XML as a string. Could it be that you need to convert the XElement into a string?
To confirm that this is the issue, why don't you try to hard code the XML body for the change state request to see if that is successful. If it is, then we know that it is due to the XML code.
Thanx,
Denise
03-08-2019 07:34 AM
Hi,
I tried it with some data types including XmlDocument, XmlNode and String.
None of them worked for me. Same Errormessage as before.
Sadly my project has been canceled due to other reasons, just wanted to give some feedback on your suggestion.
Thanks for helping.
03-08-2019 11:16 AM
Thats unfortunate. If it ever gets revitalized, come back here and I can try to help you further.
Thanx,
Denise
11-11-2021 02:04 AM
I think you are missing basic authorization type
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide