title: Use rest api call to create a new jenkins slave using python
body: <p>I am automating our jenkins slave creation process using python and i am designing a function that takes the slave name and the json file as input and creates a new jenkins slave in my jenkins instance.</p>
  
  <p>i came across one jenkins documentation which talks about this and i have a working bash script to do this. But my requirement is a python script. My bash script works but when i convert it to python it does not. Can someone help me out here.</p>
  
  <p>Here are both my <code>bash</code> and <code>python</code> scripts.</p>
  
  <pre><code>#!/bin/bash
  
  export JENKINS_URL=https://jenkins.domain.com
  export JENKINS_USER=&lt;user&gt;
  export JENKINS_API_TOKEN=&lt;api&gt;
  export NODE_NAME=testnode_sep17
  export JSON_OBJECT="{ 'name':+'${NODE_NAME}',+'nodeDescription':+'Linux+slave',+'numExecutors':+'5',+'remoteFS':+'/home/jenkins/agent',+'labelString':+'SLAVE-DOCKER+linux',+'mode':+'EXCLUSIVE',+'':+['hudson.slaves.JNLPLauncher',+'hudson.slaves.RetentionStrategy\$Always'],+'launcher':+{'stapler-class':+'hudson.slaves.JNLPLauncher',+'\$class':+'hudson.slaves.JNLPLauncher',+'workDirSettings':+{'disabled':+true,+'workDirPath':+'',+'internalDir':+'remoting',+'failIfWorkDirIsMissing':+false},+'tunnel':+'',+'vmargs':+'-Xmx1024m'},+'retentionStrategy':+{'stapler-class':+'hudson.slaves.RetentionStrategy\$Always',+'\$class':+'hudson.slaves.RetentionStrategy\$Always'},+'nodeProperties':+{'stapler-class-bag':+'true',+'hudson-slaves-EnvironmentVariablesNodeProperty':+{'env':+[{'key':+'JAVA_HOME',+'value':+'/docker-java-home'},+{'key':+'JENKINS_HOME',+'value':+'/home/jenkins'}]},+'hudson-tools-ToolLocationNodeProperty':+{'locations':+[{'key':+'hudson.plugins.git.GitTool\$DescriptorImpl@Default',+'home':+'/usr/bin/git'},+{'key':+'hudson.model.JDK\$DescriptorImpl@JAVA-8',+'home':+'/usr/bin/java'},+{'key':+'hudson.tasks.Maven\$MavenInstallation\$DescriptorImpl@MAVEN-3.5.2',+'home':+'/usr/bin/mvn'}]}}}"
  curl -L -s -o /dev/null -v -k -w "%{http_code}" -u "${JENKINS_USER}:${JENKINS_API_TOKEN}" -H "Content-Type:application/x-www-form-urlencoded" -X POST -d "json=${JSON_OBJECT}" "${JENKINS_URL}/computer/doCreateItem?name=${NODE_NAME}&amp;type=hudson.slaves.DumbSlave"
  </code></pre>
  
  <p>and here is the python script</p>
  
  <pre><code>#!/usr/bin/python
  
  import requests
  
  headers = {
      'Content-Type': 'application/x-www-form-urlencoded',
  }
  
  params = (
      ('name', 'test_sep17_TBR'),
      ('type', 'hudson.slaves.DumbSlave'),
  )
  
  #data = {
   # 'json': 'file.json'
  #}
  data = open('my_file.json', 'rb').read()
  
  response = requests.post('http://jenkins.domain.com/computer/doCreateItem', headers=headers, params=params, data=data, verify=False, auth=('user', 'xxxxxxxxxxxxxx'))
  print(response.text)
  </code></pre>
  
  <p>my_file.json has the following data</p>
  
  <pre><code>{ 'name':+'${NODE_NAME}',+'nodeDescription':+'Linux+slave',+'numExecutors':+'5',+'remoteFS':+'/home/jenkins/agent',+'labelString':+'SLAVE-DOCKER+linux',+'mode':+'EXCLUSIVE',+'':+['hudson.slaves.JNLPLauncher',+'hudson.slaves.RetentionStrategy\$Always'],+'launcher':+{'stapler-class':+'hudson.slaves.JNLPLauncher',+'\$class':+'hudson.slaves.JNLPLauncher',+'workDirSettings':+{'disabled':+true,+'workDirPath':+'',+'internalDir':+'remoting',+'failIfWorkDirIsMissing':+false},+'tunnel':+'',+'vmargs':+'-Xmx1024m'},+'retentionStrategy':+{'stapler-class':+'hudson.slaves.RetentionStrategy\$Always',+'\$class':+'hudson.slaves.RetentionStrategy\$Always'},+'nodeProperties':+{'stapler-class-bag':+'true',+'hudson-slaves-EnvironmentVariablesNodeProperty':+{'env':+[{'key':+'JAVA_HOME',+'value':+'/docker-java-home'},+{'key':+'JENKINS_HOME',+'value':+'/home/jenkins'}]},+'hudson-tools-ToolLocationNodeProperty':+{'locations':+[{'key':+'hudson.plugins.git.GitTool\$DescriptorImpl@Default',+'home':+'/usr/bin/git'},+{'key':+'hudson.model.JDK\$DescriptorImpl@JAVA-8',+'home':+'/usr/bin/java'},+{'key':+'hudson.tasks.Maven\$MavenInstallation\$DescriptorImpl@MAVEN-3.5.2',+'home':+'/usr/bin/mvn'}]}}}
  </code></pre>
  
  <p>when i run the bash script, the slave gets created.
  But when i run my python scrit, the error that i get is</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&gt;
  &lt;title&gt;Error 400 This page expects a form submission&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;&lt;h2&gt;HTTP ERROR 400&lt;/h2&gt;
  &lt;p&gt;Problem accessing /computer/doCreateItem. Reason:
  &lt;pre&gt;    This page expects a form submission&lt;/pre&gt;&lt;/p&gt;&lt;hr&gt;&lt;i&gt;&lt;small&gt;Powered by Jetty://&lt;/small&gt;&lt;/i&gt;&lt;hr/&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Edit: i can use python jenkins module to create the slave, but i do not want to use that</p>