简体   繁体   中英

Why doesn't a python patch request work for a loop?

I have a code in which I want to make a patch request for every user (see in "for user in users"). the output of the print inside the loop is this :

5ee5eee85cb7558196e1f742

101

5ee5eee85cb7558196e1f742

108

5ef9a4ec59bd2a2aabddd13d

242

5ee5eee85cb7558196e1f742

122

5ee5eee85cb7558196e1f742

129

5ef9a4ec59bd2a2aabddd13d

263

5ee5eee85cb7558196e1f742

143

5ee5eee85cb7558196e1f742

150

5ef9a4ec59bd2a2aabddd13d

284

But the patch is only made only one time and only for the first "5ee5eee85cb7558196e1f742" and the first "5ef9a4ec59bd2a2aabddd13d" and not for all the other times in the loop. Could someone help me?

extrapoints=0    
for position in positions:
    
    
    position_type = position['position_type']
        #an kapoiois user stathmeusi
    if position_type == "parking":
            #kratame tis suntetagmenes thesis gia na doume an thn exei anaferei kapoios reporter    
        coordinatesx = position['location']['coordinates'][0]
        coordinatesy = position['location']['coordinates'][1]  
            #pame sti sullogi reports na doume an uparxoun oi suntetagmenes se kapoia anafora
        for report in reports:
                #rid = report['reporter_id']
            rcoordinatesy = report['location']['coordinates'][1]    
            rcoordinatesx = report['location']['coordinates'][0]
            #extrapoints = 0   
            if rcoordinatesx==coordinatesx and rcoordinatesy==coordinatesy:
                    #an kapoia anafora exei tetoies suntetagmenes prosthetoume 7 pontous ston reporter 
                    #rid = report['reporter_id']
                rid= report['reporter_id']
                extrapoints = extrapoints + 7
                
                
                for user in users:
                    userid = user['_id']
                    rewarding_p = user['rewarding_points']
                    if userid == rid:
                        
                       
                        etag = user['_etag']
                        print (etag)
                        rewarding_p = rewarding_p + extrapoints
                        print(userid)
                        print(rewarding_p)
                        
                        data={"rewarding_points":rewarding_p}
                        headers = {'Authorization':'Bearer 8b59cc74f15befc05b48f90d5e09866028b2b624','If-Match': etag}
                        url = 'https://socialpark.iti.gr:8006/users'
                        new_url = "{}/{}".format(url, userid)
                        r = requests.patch(new_url, data, headers=headers)
                      
                        time.sleep(2)

Here you can see the r content:

{'_content': b'{"_id": "5ee5eee85cb7558196e1f742", "_updated": "Mon, 05 Oct 2020 09:22:22 GMT", "_created": "Sun, 14 Jun 2020 09:33:27 GMT", "_etag": "a97daa90626ed7493010f11d2d4be6ff06e6a7be", "_links": {"self": {"title": "user", "href": "users/5ee5eee85cb7558196e1f742"}}, "_status": "OK"}', '_content_consumed': True, '_next': None, 'status_code': 200, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:22 GMT', 'Content-Type': 'application/json', 'Content-Length': '276', 'Connection': 'keep-alive', 'ETag': '"a97daa90626ed7493010f11d2d4be6ff06e6a7be"', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8E9F68D08>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'OK', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=91716), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA297F88>}

ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 115 {'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:24 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8E9FAC6C8>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=102812), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA172088>}

16b77ead1718639c2ab727aa50c6a8a669a52da9 5ef9a4ec59bd2a2aabddd13d 263 {'_content': b'{"_id": "5ef9a4ec59bd2a2aabddd13d", "_updated": "Mon, 05 Oct 2020 09:22:26 GMT", "_created": "Mon, 29 Jun 2020 08:23:08 GMT", "_etag": "fc0363f6e2b4c59a870c06e057b09c5b94dd44f5", "_links": {"self": {"title": "user", "href": "users/5ef9a4ec59bd2a2aabddd13d"}}, "_status": "OK"}', '_content_consumed': True, '_next': None, 'status_code': 200, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:26 GMT', 'Content-Type': 'application/json', 'Content-Length': '276', 'Connection': 'keep-alive', 'ETag': '"fc0363f6e2b4c59a870c06e057b09c5b94dd44f5"', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8EA031788>, 'url': 'https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d', 'encoding': None, 'history': [], 'reason': 'OK', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=135044), 'request': <PreparedRequest [PATCH]>, 'connect ion': <requests.adapters.HTTPAdapter object at 0x000001F8E8F06C48>}

ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 129

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:28 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8E9FACC08>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=134461), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA484408>}

ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 136

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:30 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8E9FAC048>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=96633), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA031788>}

16b77ead1718639c2ab727aa50c6a8a669a52da9 5ef9a4ec59bd2a2aabddd13d 284

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:32 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8E9FACCC8>, 'url': 'https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=89694), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA484188>}

ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 150

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:34 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8EA031888>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=93464), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8E8E969C8>}

ab295b633d9c4b727a3c0342294fa463d5cfe6a2 5ee5eee85cb7558196e1f742 157

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:37 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8EA484908>, 'url': 'https://socialpark.iti.gr:8006/users/5ee5eee85cb7558196e1f742', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=152959), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA0311C8>}

16b77ead1718639c2ab727aa50c6a8a669a52da9 5ef9a4ec59bd2a2aabddd13d 305

{'_content': b'{"_status": "ERR", "_error": {"code": 412, "message": "Client and server etags don't match"}}', '_content_consumed': True, '_next': None, 'status_code': 412, 'headers': {'Server': 'nginx', 'Date': 'Mon, 05 Oct 2020 09:22:39 GMT', 'Content-Type': 'application/json', 'Content-Length': '93', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*'}, 'raw': <urllib3.response.HTTPResponse object at 0x000001F8EA0B9AC8>, 'url': 'https://socialpark.iti.gr:8006/users/5ef9a4ec59bd2a2aabddd13d', 'encoding': None, 'history': [], 'reason': 'PRECONDITION FAILED', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=116027), 'request': <PreparedRequest [PATCH]>, 'connection': <requests.adapters.HTTPAdapter object at 0x000001F8EA2DBE88>}

Logs show that you do not have permission to update the users but only the first one. You should contact the backend developer and confirm what are the conditions for updating each user.

It might be solved by using correct authorization header for each user.

More details on 412 Error. and this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM