2016-07-14 6 views
0

私はclien-server appを持っています。 私はトラブルを局所化し、これをそこにロジック:dictsのリストをrequests.postで間違っているdictの値として送信する

クライアント:

# -*- 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も取り組んでいます。しかし、サーバー側では、私は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']}> 

でのQueryDictのこの部分を何の製品を持っていない私は悲しい作る...

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

そして、私はPythonの辞書を作ってみました:

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

他のフィールドがDjangoシリアライザの検証に適していることを確認してください。しかし、私は間違った価値があるので、それはありません。

答えて

3

リクエスト(x-www-encoded-formがデフォルトであるため)のように見えます。dictのキーの値としてdictsのリストを含めることはできませんので...この場合はjsonを使用します。 は最後に、私はこの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 

私は右の応答を得ました。 解決済み

+0

この解決策はうまくいかず、「http:// url_for_working /は一重引用符を閉じていません。 – PrestonDocks

+0

@PrestonDocks fixed、thanks)) –

関連する問題