Posting XML using python requests
<p>I have the following code that posts xml to teamcity to create a new VCS root:</p>

<pre><code>def addVcsRoot(vcsRootId, vcsRootName, projectId, projectName, buildName, repoId, teamAdminUser, teamAdminPass):
     headers = {'Content-type': 'application/xml'}
     data = ("&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?&gt;" +
        "&lt;vcs-root id=\"" + vcsRootId + "\" " + "name=\"" + vcsRootName + "\" " +
        "vcsName=\"jetbrains.git\" href=\"/app/rest/vcs-roots/id:" + vcsRootId + "\"&gt;" +
        "&lt;project id=\"" + projectId + "\" " "name=\"" + projectName + "\" " "parentProjectId=\"_Root\" " +
        "description=\"Single repository for all components\" href=\"/app/rest/projects/id:" + projectId +  
        "\" " + "webUrl=\"http://teamcity.company.com/project.html?projectId=" + projectId + "\"/&gt;" +
        "&lt;properties count=\"11\"&gt;" + "&lt;property name=\"agentCleanFilesPolicy\" value=\"ALL_UNTRACKED\"/&gt;" +
        "&lt;property name=\"agentCleanPolicy\" value=\"ON_BRANCH_CHANGE\"/&gt;" +
        "&lt;property name=\"authMethod\" value=\"PASSWORD\"/&gt;" +
        "&lt;property name=\"branch\" value=\"refs/heads/master\"/&gt;" +
        "&lt;property name=\"ignoreKnownHosts\" value=\"true\"/&gt;" +
        "&lt;property name=\"submoduleCheckout\" value=\"CHECKOUT\"/&gt;" +
        "&lt;property name=\"url\" value=\"https://source.company.com/scm/" +repoId +  "/" + buildName + 
        ".git\"" + "/&gt;" + "&lt;property name=\"username\" value=\"" + teamAdminUser + "\"/&gt;" +
        "&lt;property name=\"usernameStyle\" value=\"USERID\"/&gt;" +
        "&lt;property name=\"secure:password\" value=\"" + teamAdminPass + "\"/&gt;" +
        "&lt;property name=\"useAlternates\" value=\"true\"/&gt;" + "&lt;/properties&gt;" +
        "&lt;vcsRootInstances href=\"/app/rest/vcs-root-instances?locator=vcsRoot:(id:" + vcsRootId + ")" +
        "\"" + "/&gt;" + "&lt;/vcs-root&gt;")
     url = path + 'vcs-roots'
     return requests.post(url, auth=auth, headers=headers, data=data)
</code></pre>

<p>I did a get to see what the xml file should look like and have made it so I can input different parameters for different builds, and the script works fine.  My question is: is there a more elegant way to do this?  Posting this long string with concatenation seems ugly and inefficient.  What are some other ways to post xml using requests?</p>

<p>I am not going to rewrite it all but using str.format, kwargsa and triple quoted a string will make the code a lot less cluttered:</p>

<pre><code>def addVcsRoot(**kwargs):
    headers = {'Content-type': 'application/xml'}

    data = """&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
        &lt;vcs-root id="{vcsRootId}"  name="{vcsRootName}"
        "vcsName="jetbrains.git" href="/app/rest/vcs-roots/id:"{vcsRootId}"\&gt;""".format(**kwargs)
</code></pre>

<p>Then:</p>

<pre><code>addVcsRoot(vcsRootId=1234, ......)
</code></pre>

<p>or if you want to keep the named args as is:</p>

<pre><code>def addVcsRoot(vcsRootId, vcsRootName, projectId, projectName, buildName, repoId, teamAdminUser, teamAdminPass)
    headers = {'Content-type': 'application/xml'}

    data = """&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
    &lt;vcs-root id="{vcsRootId}"  name="{vcsRootName}"
    "vcsName="jetbrains.git" href="/app/rest/vcs-roots/id:"{vcsRootId}"\&gt;"""\
    .format(vcsRootI=vcsRootId, vcsRootName=vcsRootName.....)
</code></pre>

python xml post teamcity python-requests