cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3149
Views
0
Helpful
6
Replies

Cisco Finesse Rest API C# Change User State

Julian.Postertz
Level 1
Level 1

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

6 Replies 6

dekwan
Cisco Employee
Cisco Employee

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

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;
}

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

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.

Thats unfortunate. If it ever gets revitalized, come back here and I can try to help you further.

 

Thanx,

Denise

uzair.anwar
Level 1
Level 1

I think you are missing basic authorization type