2016-07-14 9 views
0

Ich habe clien-server app. ich lokalisierte Probleme und es dieser Logik:Liste der dicts als Wert von dict mit requests.post geht schief

Auftraggeber:

# -*- coding: utf-8 -*- 
import requests 


def fixing: 
    response = requests.post('http://url_for_auth/', data={'client_id': 'client_id', 
          'client_secret':'its_secret', 'grant_type': 'password', 
          'username': 'user', 'password': 'password'}) 
    f = response.json() 
    data = {'coordinate_x': 12.3, 'coordinate_y': 8.4, 'address': u'\u041c, 12', 
      'products': [{'count': 1, 'id': 's123'},{'count': 2, 'id': 's124'}]} 
    data.update(f) 
    response = requests.post('http://url_for_working/, data=data) 
    response.text #There I have an Error about which I will say later 

OAuth2 gut arbeiten. Aber in Server-Seite habe ich keine Produkte in request.data

<QueryDict: {u'token_type': [u'type_is_ok'], u'access_token': [u'token_is_ok'], 
      u'expires_in': [u'36000'], u'coordinate_y': [u'8.4'], 
      u'coordinate_x': [u'12.3'], u'products': [u'count', u'id', u'count', 
      u'id'], u'address': [u'\u041c, 12'], u'scope': [u'read write'], 
      u'refresh_token': [u'token_is_ok']}> 

Dieser Teil von QueryDict macht mich traurig ...

'products': [u'count', u'id', u'count', u'id'] 

Und wenn ich versuchte, Python dict zu machen:

request.data.dict() 
... u'products': u'id', ... 

Und sicher andere Felder gut mit Django Serialisierer Validierung funktioniert. Aber nicht das, denn da habe ich falsche Werte.

Antwort

3

Sieht aus wie Anfrage (weil es x-www-codierte-form default) kann keine Liste von dicts als Wert für Schlüssel in dict so ... Ich sollte Json in diesem Fall verwenden. Schließlich maked ich diese func:

import requests 
import json 


def fixing: 
    response = requests.post('http://url_for_auth/', data={'client_id': 'client_id', 
         'client_secret':'its_secret', 'grant_type': 'password', 
         'username': 'user', 'password': 'password'}) 
    f = response.json() 
    headers = {'authorization': f['token_type'].encode('utf-8')+' '+f['access_token'].encode('utf-8'), 
       'Content-Type': 'application/json'} 
    data = {'coordinate_x': 12.3, 'coordinate_y': 8.4, 'address': u'\u041c, 12', 
     'products': [{'count': 1, 'id': 's123'},{'count': 2, 'id': 's124'}]} 
    response = requests.post('http://url_for_working/', data=json.dumps(data), 
           headers=headers) 
    response.text 

Es bekam ich richtige Antwort. Gelöst!

+0

Diese Lösung wird nicht funktionieren, auf einen Blick 'http: // url_for_working/fehlt ein schließendes Anführungszeichen. – PrestonDocks

+0

@PrestonDocks behoben, danke)) –

Verwandte Themen