Python-запросы эмулируют отправку CURL POST из нескольких частей с одним или несколькими файлами и телом JSON - PullRequest
0 голосов
/ 12 февраля 2019

Я взломал это уже два дня безуспешно!

Запрос WORKING CURL

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

Теперь, если я добавлю ,scrub2.zip к команде curl (отправка 2-х zip-файлов и данных JSON), я получу вывод, который выглядит следующим образом:

 ./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: 878
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW; boundary=------------------------27d684afce904423
>
< 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:20:36 GMT
< Connection: keep-alive
< Content-Type: application/json
< Content-Length: 1117
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
<
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "files": "--------------------------fd702594c1765b85\r\nContent-Disposition: attachment; filename=\"scrubbed.zip\"\r\nContent-Type: application/octet-stream\r\n\r\nZIP-CONTENT-GOES-HERE\r\n--------------------------fd702594c1765b85\r\nContent-Disposition: attachment; filename=\"scrubbed2.zip\"\r\nContent-Type: application/octet-stream\r\n\r\nZIP-CONTENT-GOES-HERE222222222\n\r\n--------------------------fd702594c1765b85--",
    "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": "878",
    "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW; boundary=------------------------27d684afce904423",
    "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

Видите ли вы разницу?Теперь эти 2 файла встроены в форму / файлы вместо файлов и формы / сообщения, отображаемых отдельно!

Этот тип запросов CURL принимается на конечной точке API Java, которая выглядит в отладчике следующим образом: The way two files look in Java debugger when CURL sends

но все мои попытки на Python, такие как:

multipart_form_data_object = {
    'scrubbed.zip': (args.files[0], open(args.files[0], 'rb'), "application/json"),
    'files': (args.files[1], open(args.files[1], 'rb'), "application/json"),
    'message': (None, open(args.message, 'rb'), 'application/json')
}
 response = requests.post(args.url + ':' + str(args.port) + '/' + args.endpoint, files=multipart_form_data_object,
                             proxies=proxies)

(что ближе всего к работе), выглядят так:

multipart_form_data_object = {
    'scrubbed.zip': (args.files[0], open(args.files[0], 'rb'), "application/json"),
    'files': (args.files[1], open(args.files[1], 'rb'), "application/json"),
    'message': (None, open(args.message, 'rb'), 'application/json')
}
response = requests.post(args.url + ':' + str(args.port) + '/' + args.endpoint, files=multipart_form_data_object,
                         proxies=proxies)

Что выводит как:

{'Content-Length': '664', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.21.0', 'Connection': 'keep-alive', 'Content-Type': 'multipart/form-data; boundary=227d4ef5a41db8a690e5cebadf336851'}
{
  "args": {},
  "data": "",
  "files": {
    "files": "ZIP-CONTENT-GOES-HERE",
    "scrubbed.zip": "ZIP-CONTENT-GOES-HERE22222"
  },
  "form": {
    "message": "{\r\n  \"runId\": \"9c4a-4c64f6d4d0ea\",\r\n  \"reports\": [\r\n    {\r\n      \"executionId\": \"d73-432a-b483-be404a13e8da\",\r\n      \"endTime\": \"\"\r\n    }\r\n  ]\r\n}"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "keep-alive",
    "Content-Length": "664",
    "Content-Type": "multipart/form-data; boundary=227d4ef5a41db8a690e5cebadf336851",
    "Host": "java.api.host.com",
    "User-Agent": "python-requests/2.21.0"
  },
  "json": null,
  "method": "POST",
  "origin": "10.0.0.2",
  "url": "http://java.api.host.com/anything"
}

Теперь, пытаясь настроить это для отправки массива файлов (в противном случае, если я переименую scrubbed.zip в files, он будет перезаписан), чтобы он выглядел так:

multipart_form_data_object = {
    'files': [(args.files[0], open(args.files[0], 'rb'), "application/json"),
     (args.files[1], open(args.files[1], 'rb'), "application/json")],
    'message': (None, open(args.message, 'rb'), 'application/json')
}

Вызывает ошибку:

Traceback (most recent call last):
  File ".\load_stress_test_endpoint.py", line 84, in <module>
    post()
  File ".\load_stress_test_endpoint.py", line 76, in post
    proxies=proxies)
  File "C:\Python\Python27\lib\site-packages\requests\api.py", line 116, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Python\Python27\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python\Python27\lib\site-packages\requests\sessions.py", line 519, in request
    prep = self.prepare_request(req)
  File "C:\Python\Python27\lib\site-packages\requests\sessions.py", line 462, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Python\Python27\lib\site-packages\requests\models.py", line 316, in prepare
    self.prepare_body(data, files, json)
  File "C:\Python\Python27\lib\site-packages\requests\models.py", line 504, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "C:\Python\Python27\lib\site-packages\requests\models.py", line 169, in _encode_files
    body, content_type = encode_multipart_formdata(new_fields)
  File "C:\Python\Python27\lib\site-packages\urllib3\filepost.py", line 90, in encode_multipart_formdata
    body.write(data)
TypeError: 'tuple' does not have the buffer interface

Моя последняя попытка была другой структурой данных (список), следующим образом:

multiple_files_list = [
    ('files', (args.files[0], open(args.files[0], 'rb'), "application/json")),
    ('files', (args.files[1], open(args.files[1], 'rb'), "application/json")),
    ('message', None, open(args.message, 'rb'), 'application/json')
]

Приводит к ошибке:

Traceback (most recent call last):
  File ".\load_stress_test_endpoint.py", line 84, in <module>
    post()
  File ".\load_stress_test_endpoint.py", line 76, in post
    proxies=proxies)
  File "C:\Python\Python27\lib\site-packages\requests\api.py", line 116, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Python\Python27\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python\Python27\lib\site-packages\requests\sessions.py", line 519, in request
    prep = self.prepare_request(req)
  File "C:\Python\Python27\lib\site-packages\requests\sessions.py", line 462, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Python\Python27\lib\site-packages\requests\models.py", line 316, in prepare
    self.prepare_body(data, files, json)
  File "C:\Python\Python27\lib\site-packages\requests\models.py", line 504, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "C:\Python\Python27\lib\site-packages\requests\models.py", line 141, in _encode_files
    for (k, v) in files:
ValueError: too many values to unpack

Не могли бы вы посоветовать, как заставить пакет запросов Python работать аналогично запросу CURL?

Ниже описано, как настраивается конечная точка Java.:

public Response index(@RequestPart("message") @Valid
                          final Message message,
                          @ApiParam(value = "Multipart File array of compressed archives (zip) ", required = true) @RequestPart("files") @Valid
                          final MultipartFile[] files)

1 Ответ

0 голосов
/ 14 февраля 2019

Ваш скрипт должен выглядеть следующим образом:

Примечание: Существует зависимость от requests_toolbelt

send.py

import argparse
import requests
from requests_toolbelt import MultipartEncoder

parser = argparse.ArgumentParser()
parser.add_argument('message')
parser.add_argument('--files', nargs='+')
args = parser.parse_args()

multipart_form_data_object = MultipartEncoder(
    fields=(
        ('files', (args.files[0], open(args.files[0], 'rb'), "application/json")),
        ('files', (args.files[1], open(args.files[1], 'rb'), "application/json")),
        ('message', ('message', open(args.message, 'rb'), 'application/json')),
    )
)

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.py

from django.urls import path
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def dump(request):
    data = {name: [o.read().decode('utf8') for o in request.FILES.getlist(name)] for name in request.FILES.keys()}
    return JsonResponse(data)

urlpatterns = [
    path('', dump),
]

Вызывается с помощью:

curl -s http://127.0.0.1:8000/ -F "message=@$(pwd)/file1" -F "files=@$(pwd)/file2" -F "files=@$(pwd)/file3"

и использование Python

python send.py file1 --files file2 file3

Тот же вывод:

{"files": ["{\\"message\\": \\"hello world\\"}\\n", "something else\\n"], "message": ["hello world\\n"]}
...