2017-12-05 4 views
1

以下の3つのテストケースGet Requests1、Get Requests2およびGet Requests3。私は、3つの異なる方法で/ RestResponse /結果ノードの値を取得しようとしましたが、それは同じエラーが表示さrobotframeworkのRequestsLibraryを使用してjsonデータにアクセスできない:TypeError:期待される文字列またはバッファ

*** Settings *** 
Library Collections 
Library OperatingSystem 
Library HttpLibrary.HTTP 
Library RequestsLibrary 

*** Test Cases *** 
Get Requests1 
    # create a HTTP session to a server 
    Create Session countryname http://services.groupkt.com 
    # store response 
    ${resp}= Get Request countryname /country/get/all 
    log ${resp.content} 
    # ${resp.content} display entire json response, now tring to get node value of /RestResponse/result 
    ${getResponseJson} Get Json Value ${resp} /RestResponse/result 
    # shows error TypeError: expected string or buffer 
    log ${getResponseJson.content} 

Get Requests2 
    # create a HTTP session to a server 
    Create Session countryname http://services.groupkt.com 
    # store response 
    ${resp}= Get Request countryname /country/get/all 
    log ${resp.content} 
    # ${resp.content} display entire json response, now tring to get node value of /RestResponse/result 
    ${responseContent}= to json ${resp.content} 
    # shows error TypeError: expected string or buffer 
    ${getResponseJson} Get Json Value ${responseContent} /RestResponse/result 
    log ${getResponseJson.content} 


Get Requests3 
    # create a HTTP session to a server 
    Create Session countryname http://services.groupkt.com 
    # store response 
    ${resp}= Get Request countryname /country/get/all 
    log ${resp.content} 
    # ${resp.content} display entire json response,parse json 
    ${data} Parse Json ${resp} 
    # shows error TypeError: expected string or buffer 
    ${getResponseJson} Get Json Value ${resp} /RestResponse/result 

JSONファイル応答

あなたがいない要求オブジェクトから、JSONから Get Json Valueを呼び出す必要が
{ 
    "RestResponse": { 
     "messages": [ 
      "Total [249] records found." 
     ], 
     "result": [ 
      { 
       "name": "Afghanistan", 
       "alpha2_code": "AF", 
       "alpha3_code": "AFG" 
      }, 
      { 
       "name": "\ufffd\ufffdland Islands", 
       "alpha2_code": "AX", 
       "alpha3_code": "ALA" 
      }, 
      { 
       "name": "Albania", 
       "alpha2_code": "AL", 
       "alpha3_code": "ALB" 
      }, 
      { 
       "name": "Algeria", 
       "alpha2_code": "DZ", 
       "alpha3_code": "DZA" 
      } 
     ] 
    } 
} 

答えて

2

。あなたのコードでは、${resp}は、JSONデータとjsonパーサーが知らない他のものを含むオブジェクトです。

${getResponseJson} Get Json Value ${resp.content} /RestResponse/result 

${resp}はpythonオブジェクトです。これにはjsonデータがありますが、HTTP戻りコードなどの情報もあります。 JSONを受け入れるものには渡すことはできません。

${resp.content}は、HTTP応答の本体です。あなたのコメントを書いているように、これはJSONデータです。 JSONデータを受け入れるキーワードはすべてこれを受け入れる必要があります。

${resp.json}は、のJSON文字列で、Pythonオブジェクトに変換されています。これはもはやJSONではなく、Pythonの辞書です。これをJSONを必要とする関数に渡すことはできません。しかし、通常のPython辞書のように扱うことができます。

コードを実行すると、${getResponseJson}に期待するデータがあります。これはユニコード文字列であり、ユニコード文字列にはcontent属性がありません。

+0

返信いただきありがとうございます。 $ {resp.content}で試しました。 $ {getResponseJson.content} 'が失敗しました:AttributeError:' unicode 'オブジェクトに' content '属性がありません。私も$ {resp.json}で試して、TypeError:期待される文字列またはバッファーを表示しました。 – madhur

+0

@madhur:入力したものと入力したものと自分の結果を詳しく見てください。あなたのコメントでは、 '$ {resp.content}'にjson文字列が表示されます。その文字列は、他のデータを検索するために必要なものです。 '$ {resp.json} 'ではなく' $ {getResponseJson.content} 'ではありません。 –

+0

間違いなく、$ {resp.content}にはjson文字列が表示されます。ライブラリメソッドは、この変数にアクセスしようとすると、問題に言及したようにエラーを表示しています。 – madhur