2015-06-17 53 views
2

使用wslite.rest.RestClient,如果我使用postput,則會收到服務返回的411長度要求錯誤。我添加了標題Content-Length: (size),但仍然出現錯誤。有沒有人有提議?下面是一個put請求代碼:Groovy wslite.rest.RestClient發佈或將結果放入411長度所需的錯誤

 def builder = new JsonBuilder() 
     // required json data 
     def root = builder { 
      "ActivationDate" "\\/Date(1434563608000-0500)\\/" 
      "EmailAddress" "[email protected]" 
      "ExpirationDate" "\\/Date(1435686808000-0500)\\/" 
      "FirstName" "ebaa" 
      "LastName" "ebaa" 
      "MiddleName" "ebaa" 
      "OtherName" "ebaa" 
      "Password" "abc12345" 
      "Status" 1 
     } 

     RESTClient restClient = new RESTClient('https://serviceBaseUrl') 
     Response response 

     try { 
      restClient.authorization = new HTTPBasicAuthorization(username: 'user', password: 'pass') 
      restClient.defaultCharset = 'UTF-8' 
      restClient.defaultContentTypeHeader = 'application/json' 
      restClient.defaultAcceptHeader = 'application/json' 

      response = restClient.put(path: "/Location/${locName}/Administrator/${name}", 
       headers:['Accept': 'application/json', 
        'Accept-Language':'en-US,en;q=0.5', 'Content-Type': 'application/json; charset=UTF-8', 
        'Connection':'keep-alive', 'Pragma':'no-cache', 'Cache-Control':'no-cache', 
        'Content-Length': builder.toString().length()], 
        data: builder.toPrettyString().getBytes()) 

      return response.json 
     } catch(ex) { 
      ex.printStackTrace() 
     } 

我也試圖改變數據:參數去的身體,但我得到了同樣的答覆。另外,如果我使用Firefox插件HttpRequester(https://addons.mozilla.org/En-us/firefox/addon/httprequester/)併發出相同的請求,我會得到一個200狀態碼並更新相應的數據。謝謝!

回答

4

對於putpost它期望有效載荷處於關閉狀態。請嘗試以下,這應該發送數據,並自動設置正確的Content-Length

.... 
.... 
response = restClient.put(
       path: "/Location/${locName}/Administrator/${name}", 
       headers:['Accept': 'application/json', 
         'Accept-Language':'en-US,en;q=0.5', 
         'Content-Type': 'application/json; charset=UTF-8', 
         'Connection':'keep-alive', 
         'Pragma':'no-cache', 
         'Cache-Control':'no-cache']) 
    { 
     text builder.toPrettyString()    
     //bytes builder.toPrettyString().bytes // or as bytes 
     //json 'ActivationDate': '...', 'EmailAddress': '...' // or a json string from a map 
    } 

見自述的Sending Content部分。

+0

完美!當我早些時候嘗試過時,我收到了「400錯誤請求」。似乎我的逃脫反斜槓\\不被服務器在日期附近所喜歡。感謝幫助!謝謝約翰! –

相關問題