fsprojects / FsHttp

A lightweight F# HTTP library by @SchlenkR and @dawedawe
https://fsprojects.github.io/FsHttp/
Apache License 2.0
424 stars 42 forks source link

Allow filename metadata with other "parts" like bytearray or stream #106

Closed lithiumfrost closed 11 months ago

lithiumfrost commented 1 year ago

I am using the following code with Telegram:

http {
            POST $"https://api.telegram.org/bot%s{apiTelegramKey}/sendPhoto"

            query [ "chat_id", telegramChatId
                    "caption", caption
                    "parse_mode", "MarkdownV2" ]

            multipart
            part (ContentData.ByteArrayContent weather) (Some "image/jpeg") "photo"

And it seems to hit that bot API correctly, a filename parameter is required in addition to the fieldname (photo).

Is there a way to do this that doesn't rely on having to save the byte[] or stream to disk and then use the existing filePart DU?

SchlenkR commented 1 year ago

Hi @lithiumfrost - sorry for the late reply. I'll have a look at this soon...

SchlenkR commented 1 year ago

@lithiumfrost I don't understand the question. What exactly should be possible? In general, it's not neseccary to have anything saved to disk. Or: Is the question about content type "guessing", so that (Some "image/jpeg") could omitted?

lithiumfrost commented 1 year ago

Happy to explain.

The MIME "part" in a multipart form-data submission has multiple pieces of metadata:

------WebKitFormBoundaryN8Y1MQiNiQs7fEEd
Content-Disposition: form-data; name="images"; filename="testfile1.txt"
Content-Type: text/plain

Hello World, no trailling newlines
------WebKitFormBoundaryN8Y1MQiNiQs7fEEd

I can provide the mime-type, but I can't provide the filename value in the header (the third header value in the example) which is required by telegram in the API call. I believe that it's the same specification as a web browser has.