Good day! There is the following code:

for i in json_data['data']: if i['user'] == 'user': time = (i['dur']) start_time= (i['start']) task = (i['task']) client = (i['client']) project = (i['project']) description = (i['description']) d = (xtemptime/1000) hours, remainder = divmod(d, 3600) minutes, seconds = divmod(remainder, 60) hh = ("%d" % (hours)) mm = ("%d" % (minutes)) request = u"""<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Insert xmlns="http://tempuri.org/"> <workdate>{start_time}</workdate> <customer>{client}</customer> <project>{project}</project> <workname>{description}</workname> <hours>{hh}</hours> <minutes>{mm}</minutes> <comments></comments> <worktype>1</worktype> </Insert> </soap:Body> </soap:Envelope>""".format(start_time=start_time, client=client, project=project, description=description, hh=hh, mm=mm) encoded_request = request.encode('utf-8') authenticationHeader = { "Host": "host", "Content-Type": "text/xml; charset=UTF-8", "Content-Length": len(encoded_request) } response = requests.post(url="http:url", auth=HttpNtlmAuth('',''), headers = authenticationHeader, data = encoded_request, verify=False) 

All data is taken from json_data = r.json() which is higher and in general everything is fine with it.

The loop (for i in json_data['data']:) works fine when it receives several entries at the input, but only request = u"""... (last) gets into request = u"""... .

An example of how it looks now (if you arrange print i and print request ):

 ~]# python test.py (1800000, u'2016-09-10T12:00:00+03:00', None, u'test2', u'test2', u'test2') (24296000, u'2016-09-10T11:35:44+03:00', None, u'test', u'test', u'test') <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Insert xmlns="http://tempuri.org/"> <workdate>2016-09-10T11:35:44+03:00</workdate> <customer>test</customer> <project>test</project> <workname>test</workname> <hours>1</hours> <minutes>1</minutes> <comments></comments> <worktype>1</worktype> </Insert> </soap:Body> </soap:Envelope> ~]# 

How to make request = u""".. Get all the records going through the for loop, and then requests.post send them? As you can see, what applies to test2 did not fall into the request .

Maybe I generally go in the wrong direction, and it is better to do something else?

    1 answer 1

    The code does exactly what they wrote in it. At each turn of the loop, the request variable is overwritten by the new value. And you need to the fact that there has already fallen on the previous turns, append new information.

    It is necessary before the start of the loop to declare request, writing there that general header, which will be only one and does not depend on the number of elements:

     request = u"""<?xml version="1.0" encoding="utf-8"?>""" 

    (I don’t know if the following tag should be common to all elements, or for each individual one — you can see for yourself and add it either here or inside the loop)

    Inside the loop, you add to this variable the value that will be separate for each element.

     request += u"""здесь_какой_то_xml_код""" 

    Please note that here we are using not just the "equal" symbol, but the "plus-equal" combination - this allows us not to overwrite the old value, but to add a new value to it.

    If necessary, after the cycle, you need to add to the variable another common closing part of the query, if it exists.

    NB: Actually, it is more correct to use here not + =, but concatenation via join, but I am afraid that it will be difficult for you, and in general this is a separate topic. In your case, there should not be a big loss in performance, you can use + =

    • Yes indeed, now everything gets requests , that's great. But how, in this case, to ensure that when sending (requests post) records were sent in turn, and not in one? - sirin zarin
    • If you want them to leave one by one, then you initially set the wrong task. It turns out that you do not need to accumulate all requests in one variable. Make them in a loop as you originally had, with the difference that you need to send them right away without leaving the loop. But the server responses (if you need them) will have to somehow be accumulated in order to be processed later. - Xander
    • You are absolutely right, I apologize for the inaccuracy in the production. I did it without leaving the cycle, and everything worked out. - sirin zarin
    • @sirinzarin in the general case, when you get to know the language closer, you can also generate xml using objects (such as xml.etree.ElementTree ) and not collect from strings, which easily leads to errors. Although, if you have to work with soap (my condolences), where often the server in the selected stack understands only a very specific magic format, then creating xml formatting of a string template can be a practical idea. - jfs