foss42 / apidash

API Dash is a beautiful open-source cross-platform API Client built using Flutter which can help you easily create & customize your API requests, visually inspect responses and generate API integration code. A lightweight alternative to postman/insomnia.

Home Page:https://apidash.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add multipart for Python

nikkivirtuoso opened this issue · comments

Describe the bug/problem
the code generated for python(both) is not correct for multipart having type json and text

Steps to Reproduce the bug/problem
paste a api link
click on view code
select any python request
choose formdata
the code that will appear is:

import requests
import mimetypes
from codecs import encode

url = 'http://nishant'

def build_data_list(fields):
    dataList = []
    for field in fields:
        name = field.get('name', '')
        value = field.get('value', '')
        type_ = field.get('type', 'text')

        dataList.append(encode('--20a87120-372b-1f03-87bb-d71ea3913925'))
        if type_ == 'text':
            dataList.append(encode(f'Content-Disposition: form-data; name="{name}"'))
            dataList.append(encode('Content-Type: text/plain'))
            dataList.append(encode(''))
            dataList.append(encode(value))
        elif type_ == 'file':
            dataList.append(encode(f'Content-Disposition: form-data; name="{name}"; filename="{value}"'))
            dataList.append(encode(f'Content-Type: {mimetypes.guess_type(value)[0] or "application/octet-stream"}'))
            dataList.append(encode(''))
            dataList.append(open(value, 'rb').read())
    dataList.append(encode('--20a87120-372b-1f03-87bb-d71ea3913925--'))
    dataList.append(encode(''))
    return dataList

dataList = build_data_list([{"name":"Nishant","value":"","type":"text"}])
payload = b'\r\n'.join(dataList)
response = requests.post(url, data=payload, json=json.loads(payload), headers=headers)

print('Status Code:', response.status_code)
print('Response Body:', response.text)

Expected behavior

import requests
import mimetypes
import json  # Added import for the json module

url = 'http://nishant'

def build_data_list(fields):
    dataList = []
    for field in fields:
        name = field.get('name', '')
        value = field.get('value', '')
        type_ = field.get('type', 'text')

        dataList.append(encode('--a4902730-3203-1f03-87bb-d71ea3913925'))  # Adjust boundary string
        if type_ == 'text':
            dataList.append(encode(f'Content-Disposition: form-data; name="{name}"'))
            dataList.append(encode('Content-Type: text/plain'))
            dataList.append(encode(''))
            dataList.append(encode(value))
        elif type_ == 'file':
            dataList.append(encode(f'Content-Disposition: form-data; name="{name}"; filename="{value}"'))
            dataList.append(encode(f'Content-Type: {mimetypes.guess_type(value)[0] or "application/octet-stream"}'))
            dataList.append(encode(''))
            dataList.append(open(value, 'rb').read())
    dataList.append(encode('--a4902730-3203-1f03-87bb-d71ea3913925--'))
    dataList.append(encode(''))
    return dataList

dataList = build_data_list([{"name":"asfacs","value":"acsacac","type":"text"}])
payload = b'\r\n'.join(dataList)
response = requests.post(url, data=payload, headers=headers)

print('Status Code:', response.status_code)
print('Response Body:', response.text)

here i have used arbitrary headers and api request and input json and text data for simplification only
Device Info (The device where you encountered this issue):

  • OS: [e.g. Windows, MacOS]
  • Version: [e.g. Catalina 10.15.7, Monterey 12.3.1, Windows 11 22H2]
  • Browser (only if you encountered the issue while running the web app): [e.g. chrome, safari]

@nikkivirtuoso You need to specify within code blocks what is the generated code and what is the expected code.

@nikkivirtuoso why have you closed the issue?

@animator In most cases, payloads are not sent as JSON unless explicitly requested by the API. The choice of payload format depends on the requirements specified by the API endpoint.

response = requests.post(url, data=payload, json=json.loads(payload), headers=headers)

we can remove the json right ?

response = requests.post(url, data=payload, headers=headers)

No. Python library supports this feature which is very useful and we want to provide users with generated code which is as natural as possible and which uses the full capabilities of the library.

Now I am left with the file handling part, there is no file handling done in the code @animator

there is one line missing for file handling part:

with open(value, 'rb') as f:
                file_content = f.read()

in the code

The issue has been resolved in the main branch.