猿问

Python 请求模拟 CURL POST 发送带有 1 个或多个文件和 JSON 正文的多部分请求

我已经破解了两天了,没有运气!


工作卷曲请求


curl -X POST -v "http://$1:8080/controller/endpoint" -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "message={ \"id\": \"b3562c86-6ff4-4bf7-9c4a-4c64fff4d0ea\", \"stuff\": [

{

\"id\": \"1ca2d9b1-1d73-432a-b483-be404afff8da\",

.......

\"endTime\": \"\"

}]}};type=application/json" -F "files=@file.zip"

返回如下所示的输出:


 ./rest.sh http://127.0.0.1/anything

* Hostname was NOT found in DNS cache

*   Trying 127.0.0.1...

* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)

> POST /anything HTTP/1.1

> User-Agent: curl/7.35.0

> Host: 127.0.0.1

> Accept: */*

> Cache-Control: no-cache

> Content-Length: 493

> Expect: 100-continue

> Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW; boundary=------------------------52912a6946761b42

>

< HTTP/1.1 100 Continue

< HTTP/1.1 200 OK

* Server gunicorn/19.9.0 is not blacklisted

< Server: gunicorn/19.9.0

< Date: Tue, 12 Feb 2019 18:18:56 GMT

< Connection: keep-alive

< Content-Type: application/json

< Content-Length: 725

< Access-Control-Allow-Origin: *

< Access-Control-Allow-Credentials: true

<

{

  "args": {},

  "data": "",

  "files": {

    "files": "ZIP-CONTENT-GOES-HERE"

  },

  "form": {

    "message": "{ \"runId\": \"1ca2d9b1-1d73-432a-b483-be404a13e8da\", \"reports\": [\n{\n\"executionId\": \"1ca2d9b1-1d73-432a-b483-be404a13e8da\",\n\"endTime\": \"\"\n}]}}"

  },

  "headers": {

    "Accept": "*/*",

    "Cache-Control": "no-cache",

    "Content-Length": "493",

    "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW; boundary=------------------------52912a6946761b42",

    "Expect": "100-continue",

    "Host": "127.0.0.1",

    "User-Agent": "curl/7.35.0"

  },

  "json": null,

  "method": "POST",

  "origin": "172.17.42.1",

  "url": "http://127.0.0.1/anything"

}

* Connection #0 to host 127.0.0.1 left intact


慕仙森
浏览 327回答 1
1回答

三国纷争

您的脚本应如下所示:注意:有依赖requests_toolbeltsend.pyimport argparseimport requestsfrom requests_toolbelt import MultipartEncoderparser = argparse.ArgumentParser()parser.add_argument('message')parser.add_argument('--files', nargs='+')args = parser.parse_args()multipart_form_data_object = MultipartEncoder(&nbsp; &nbsp; fields=(&nbsp; &nbsp; &nbsp; &nbsp; ('files', (args.files[0], open(args.files[0], 'rb'), "application/json")),&nbsp; &nbsp; &nbsp; &nbsp; ('files', (args.files[1], open(args.files[1], 'rb'), "application/json")),&nbsp; &nbsp; &nbsp; &nbsp; ('message', ('message', open(args.message, 'rb'), 'application/json')),&nbsp; &nbsp; ))res = requests.post('http://localhost:8000', data=multipart_form_data_object, headers={'Content-Type': multipart_form_data_object.content_type})print(res.content)我使用 django 对其进行了测试:urls.pyfrom django.urls import pathfrom django.http import JsonResponsefrom django.views.decorators.csrf import csrf_exempt@csrf_exemptdef dump(request):&nbsp; &nbsp; data = {name: [o.read().decode('utf8') for o in request.FILES.getlist(name)] for name in request.FILES.keys()}&nbsp; &nbsp; return JsonResponse(data)urlpatterns = [&nbsp; &nbsp; path('', dump),]使用以下方法调用它:curl -s http://127.0.0.1:8000/ -F "message=@$(pwd)/file1" -F "files=@$(pwd)/file2" -F "files=@$(pwd)/file3"并使用pythonpython send.py file1 --files file2 file3相同的输出:{"files": ["{\\"message\\": \\"hello world\\"}\\n", "something else\\n"], "message": ["hello world\\n"]}
随时随地看视频慕课网APP

相关分类

Java
我要回答