2016-08-15 49 views
1

我試圖創建一個使用paypal REST API付款,但我不斷收到此錯誤響應:寶REST API不斷返回畸形的JSON錯誤

{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"262cbdc417df7"} 

在這裏與我的代碼:

payment_url = 'https://api.sandbox.paypal.com/v1/payments/payment' 
headers = {"Content-Type": "application/json", "Authorization": "Bearer %s" % access_token} 
data = { 
     "intent": "sale", 
     "redirect_urls": { 
      "return_url": "http://localhost:8080/index.html", 
      "cancel_url": "http://localhost:8080/index.html" 
     }, 
     "payer": { 
      "payment_method": "paypal" 
     }, 
     "transactions": [ 
      { 
       "amount": { 
        "total": "7.47", 
        "currency": "USD" 
       }, 
       "details": { 
        "subtotal": "7.41", 
        "tax": "0.03", 
        "shipping": "0.03" 
       }, 
       "description": "This is the payment transaction description.", 
       "item_list": { 
        "items": [ 
         { 
          "quantity": "1", 
          "name": "item", 
          "price": "7.41", 
          "currency": "USD", 
          "sku": "item" 
         }] 
       } 
      } 
     ] 
    } 
    print headers 
    print data 
    r = requests.post(payment_url, headers=headers, data=data) 
    print 'payment res', r.text 

而且我只得到這樣的迴應:

{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"262cbdc417df7"} 

我已經看到了很多關於這種錯誤,但他們都沒有解決方案。 :( json格式的post數據顯然是有效的,否則,請求post方法會引發異常,並且從paypal服務器返回響應,但是我找不到來自它提供的鏈接的任何信息。 ,我想我正是提出的要求作爲樣品。我錯過了什麼?

任何一種意見或解決方案將不勝感激。

回答

0

return_urlcancel_url值需要有他們周圍報價。正如消息所述,您的JSON格式不正確。

試試這個 - http://jsonlint.com/看到你的錯誤。

+0

嗨,甘道夫,謝謝你的回答。值return_url和cancel_url是字符串變量,並且有引號。 json是有效的,否則post請求將失敗的參數。我認爲問題是,對paypal api的帖子要求已經完成,但有一個奇怪的迴應,我沒有線索。 – MrAZ