Python Forum

Full Version: Need help with posting to API website
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HI ,

I am pretty new to python and wanted help with below code? All I am trying to do is to make a project name and post it to API website. Not sure if my code is correct? This is not working, I don’t grt any error s but the code look slike by passess the createPRoject portion and ends with no errors?

Process finished with exit code 0

Thanks a bunch!
Pasi
import datetime

from pip._vendor import requests

import requests

import urllib2

import glsapiutil

import urllib

import _auth_tokens

globals()

TODAY = ""





def getToday():

   now = datetime.datetime.now()



   strToday = str(now.year) + "-"

   tmp = str(now.month)

   if len(tmp) == 1:

       tmp = "0" + tmp

   strToday += (tmp + "-")

   tmp = str(now.day)

   if len(tmp) == 1:

       tmp = "0" + tmp

   strToday += tmp



   return strToday



encoding = "utf-8"





def createProject (pName, udfs , pDate):

   print (pName, udfs, pDate)

#    print(pName, udfs, pDate)

   global r

   global post_response

   global d

   r.encoding = "utf-8"
   pName = "Prj123"
   udfs = “UT231”
   pDate = getToday()

   url1Project = "mytestdotcom/ri/project"
   urludfs = ' mytestdotcom/ri/userdefined'
   pName = "Prj123"

   post_data = {'username': "ra", 'password': "mypasswrd"}
   post_response = requests.post("testdotcom/api/v2/projects", data=post_data)
   r = requests.post(url1Project, data=createProject)
   print (r.text)

#  print response


def main():
         global api
   #     global ARGS
         api = glsapiutil.glsapiutil()
#          api.setup(_auth_tokens.API_USERNAME, _auth_tokens.API_PASSWORD)

#     #api.setURI(args.stepURI)
#     #api.setup(args.username, args.password)
#     global TODAY
#     api = glsapiutil.glsapiutil()
if __name__ == "__main__":
   main()
If you have formatting in your code, we can't copy it and run it to see what the problem is.

Without running it, I would say that I don't see where you call the createProject function. You have to call a function after you create it to get it to do anything, like you call main.

The global statement should not be used here. You should return values from your function (with a return statement) instead.
In addition,
  • Use now.strftime() to produce the string you need
  • Use the requests module instead of urllib2
(Feb-15-2017, 01:51 AM)ichabod801 Wrote: [ -> ]If you have formatting in your code, we can't copy it and run it to see what the problem is.

Without running it, I would say that I don't see where you call the createProject function. You have to call a function after you create it to get it to do anything, like you call main.

The global statement should not be used here. You should return values from your function (with a return statement) instead.

HI, I am not sure what you mean by formatting , I dont think I have formatting, but cant you copy paste this in your editor? Like I said I am new to python and I thought I have defined the function createProject, so if you could please fix ( give me  an example within this code) what I done wrong and send it back to me so I can run it and see
Thanks.
Pasi