YaleShen
Contributor
Contributor

Issue an REST API Call Through C# For "Modify a Fixed Cost Value"

Jump to solution

Hi -

I am trying to issue an REST API Call through C# for " modifying a fixed cost value".

I have successfully done the following tasks through C# with REST API call on chargeback manager 2.0

-     Login the chargeback manager 2.0

-     Add a Custom Chargeback Hierarchy

-     Add vCenter Server Entity to the Chargeback Hierarchy which has been created by the REST API

-     Add Fixed Cost Model.

But When i tried to use the REST API call to do " modify a fixed cost value" based on the API programming document version 2.0.

It is not successful ...Following is the code and error messages i got.

Code:

public static string ModifiyFixedCost(CookieContainer cookieContainer)
        {
            HttpWebRequest request;
            Stream streamReq;
            request = (HttpWebRequest)WebRequest.Create("https://10.63.11.34/vCenter-CB/api/fixedCost/707/values");
            //request = (HttpWebRequest)WebRequest.Create("https://10.63.11.34/vCenter-CB/api/hierarchy/696/entity/697/costModel/701/fixedCosts/values");
            request.CookieContainer = cookieContainer;
            request.Method = "PUT";
            request.KeepAlive = true;
            request.Timeout = 600000;
            request.ContentType = "application/x-www-form-urlencoded";//application/x-www-form-urlencoded
            streamReq = request.GetRequestStream();

            ///Read XML file.
            String strXML;
            string strCurrentPath = System.IO.Directory.GetCurrentDirectory();
            string strPath = "";
            strPath = strCurrentPath + "\\ModifiyFixedCostXML.txt";

            using (StreamReader sr = new StreamReader(strPath))
            {
                strXML = sr.ReadToEnd();
            }

            string strResp;
            byte[] byteArray = Encoding.UTF8.GetBytes(strXML);
            streamReq.Write(byteArray, 0, byteArray.Length);
            streamReq.Close();

            using (WebResponse response = request.GetResponse())
            {
                Stream streamResp = response.GetResponseStream();
                StreamReader streamReaderResp = new StreamReader(streamResp);
                strResp = streamReaderResp.ReadToEnd();
            }
            return strResp;
        }

XML File:

<?xml version="1.0" encoding="UTF-8"?>
<Request>
<FixedCosts>
<FixedCost id="1">
<Values>
<Value>
<Cost>3.14</Cost>
<Duration id="1"/>
</Value>
</Values>
</FixedCost>
</FixedCosts>
</Request>

Error Messages;

<?xml version="1.0" encoding="UTF-8"?>

<Response xmlns="http://www.vmware.com/vcenter/chargeback/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" status="failure">

<Error majorErrorCode="500" minorErrorCode="0" message="No such method defined or API call missing required arguments."/>

</Response>

Please advise what is problem here ...Is the chargeback REST API 2.0 support C# ?

Thanks

Yale

0 Kudos
1 Solution

Accepted Solutions
dprabhakaran
Enthusiast
Enthusiast

Yale,

  I see following changes are required in the request:

  1. URL is missing the startTime and endTime, if endTime is not required send it as -1. Eg: http://localhost:8080/vCenter-CB/api/fixedCost/553/values?startTime=1340130600000&endTime=-1
  2. In XML fixed cost id should be same as mentioned in URL. XML tag is <FixedCost>

Thanks

Diwakar

View solution in original post

0 Kudos
2 Replies
dprabhakaran
Enthusiast
Enthusiast

Yale,

  I see following changes are required in the request:

  1. URL is missing the startTime and endTime, if endTime is not required send it as -1. Eg: http://localhost:8080/vCenter-CB/api/fixedCost/553/values?startTime=1340130600000&endTime=-1
  2. In XML fixed cost id should be same as mentioned in URL. XML tag is <FixedCost>

Thanks

Diwakar

0 Kudos
YaleShen
Contributor
Contributor

Diwakar -

Thanks for your answer ...it works now based on your advise

0 Kudos