简体   繁体   中英

CURL -F to python requests post

I read the Paypal API documents:

curl -v -X POST https://api.sandbox.paypal.com/v1/customer/disputes/PP-D-27803/provide-evidence \
-H "Content-Type: multipart/related; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" \
-H "Authorization: Bearer Access-Token" \
-F 'input={
  "evidences": [
{
  "evidence_type": "PROOF_OF_FULFILLMENT",
  "evidence_info": {
  "tracking_info": [
    {
    "carrier_name": "FEDEX",
    "tracking_number": "122533485"
    }
  ]
  },
  "notes": "Test"
}
  ]
};type=application/json' \
-F 'file1=@NewDoc.pdf'

I don't know how to covert the -F 'input: ....' to python requests post code. It always report error:

{u'name': u'VALIDATION_ERROR', u'links': [], u'details': [{u'issue': u'MISSING_OR_INVALID_REQUEST_BODY', u'location': u'body'}], u'message': u'Invalid request - see details', u'information_link': u'https://developer.paypal.com/docs/api/customer-disputes/#errors', u'debug_id': u'd9fa3425a02fe'}

There's a cool website that can do it for (since you provided curl command for it) https://curl.trillworks.com/

output

import requests

headers = {
    'Content-Type': 'multipart/related; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
    'Authorization': 'Bearer Access-Token',
}

files = {
    'input': (None, '{\n  "evidences": [\n{\n  "evidence_type": "PROOF_OF_FULFILLMENT",\n  "evidence_info": {\n  "tracking_info": [\n    {\n    "carrier_name": "FEDEX",\n    "tracking_number": "122533485"\n    }\n  ]\n  },\n  "notes": "Test"\n}\n  ]\n};type'),
    'file1': ('NewDoc.pdf', open('NewDoc.pdf', 'rb')),
}

response = requests.post('https://api.sandbox.paypal.com/v1/customer/disputes/PP-D-27803/provide-evidence', headers=headers, files=files)

This should give you an idea of how its done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM