1

I am new to python so please forgive my lack of conscience..

Requirement : Need to process a list of strings through a rest api call

Problem: Using Requests, I am able to successfully make the api calls and get the work done. No problems using Requests except wanted to research if using session will expedite my processing as I am passing AUTH in each request.

Followed multiple leads on using session but continue to get HTTP 400 - Bad Request.

Referencing: Python Requests and persistent sessions

Here is the code that I am trying to make it work and any assistance will be very helpful:

import requests
from requests.auth import HTTPBasicAuth

with open('list.txt') as f:
    lines = f.read().splitlines()

s = requests.Session()
r = s.get("https://ucdeploy.domain.com/rest/state", auth=HTTPBasicAuth('username', 'password'))

if r:
    print("Logged in Successfully")
else:
    print("Login Failed ==> " + str(r))
    exit()

for cmp in lines:
    print("Processing - " + cmp.strip())
    payload = '{"name":"' + cmp.strip() + '","description":"","templateId":"141e248c-ca02-4f51-a873-c07acd5366cd",' \
                                          '"templateVersion":"","componentType":"STANDARD","sourceConfigPlugin":"",' \
                                          '"importAutomatically":"false","useVfs":"true","defaultVersionType":"FULL",' \
                                          '"importAgentType":"inherit","inheritSystemCleanup":"true",' \
                                          '"runVersionCreationProcess":"false","properties":{},"teamMappings":[{' \
                                          '"teamId":"bf3c4566-160b-4d79-b47e-5f2bbc1ffbb0"}]}'

    response = s.put("https://ucdeploy.domain.com/rest/deploy/component", data=payload)
    if response:
        print("Successfully Processed")
    else:
        print("Failed ==> " + str(response))
        exit()

Output:

Logged in Successfully
Processing - Component1
Failed ==> <Response [400]>
Dinesh
  • 41
  • 5
  • But the same payload works well with requests This code works with no issues: `response = requests.put("https://ucdeploy.domain.com/rest/deploy/component", data=payload, auth=HTTPBasicAuth('username', 'password'))` – Dinesh Sep 17 '19 at 20:32

1 Answers1

0

Identified the problem with the payload formatting.

For some reason the concatenation code below is not working:

payload = '{"name":"' + cmp.strip() + '","description":"","templateId":"141e248c-ca02-4f51-a873-c07acd5366cd",' \
                                          '"templateVersion":"","componentType":"STANDARD","sourceConfigPlugin":"",' \
                                          '"importAutomatically":"false","useVfs":"true","defaultVersionType":"FULL",' \
                                          '"importAgentType":"inherit","inheritSystemCleanup":"true",' \
                                          '"runVersionCreationProcess":"false","properties":{},"teamMappings":[{' \
                                          '"teamId":"bf3c4566-160b-4d79-b47e-5f2bbc1ffbb0"}]}'

Updated the payload concatenation with following and it works:

componentname = '{"name":"' + cmp.strip()
payload = componentname + '","description":"","templateId":"141e248c-ca02-4f51-a873-c07acd5366cd",' \
                              '"templateVersion":"","componentType":"STANDARD","sourceConfigPlugin":"",' \
                              '"importAutomatically":"false","useVfs":"true","defaultVersionType":"FULL",' \
                              '"importAgentType":"inherit","inheritSystemCleanup":"true",' \
                              '"runVersionCreationProcess":"false","properties":{},"teamMappings":[{' \
                              '"teamId":"bf3c4566-160b-4d79-b47e-5f2bbc1ffbb0"}]}'

I hope this helps someone.

Dinesh
  • 41
  • 5