CreateFormData - Creates "multipart/form-data" for http post

Post your working scripts, libraries and tools
tmplinshi
Posts: 1090
Joined: 01 Oct 2013, 14:57

CreateFormData - Creates "multipart/form-data" for http post

13 May 2015, 16:22

CreateFormData v1.20 (2016-6-17)

Source

Functions:

  • CreateFormData(ByRef retData, ByRef retHeader, objParam)
    • retData - (out) Data used for HTTP POST.
    • retHeader - (out) Content-Type header used for HTTP POST.
    • objParam - (in) An object defines the form parameters. To specify files, use array as the value.

      Example
  • CreateFormData_WinInet(ByRef retData, ByRef retHeader, objParam)
    Used for VxE's HTTPRequest().


Example:
Upload multiple images to postimage.org


ChangeLog:
1.20 / 2016-6-17 - Added CreateFormData_WinInet(), which can be used for VxE's HTTPRequest().
1.10 / 2015-6-23 - Fixed a bug
1.00 / 2015-5-14
Last edited by tmplinshi on 17 Jun 2016, 04:25, edited 7 times in total.
User avatar
Pulover
Posts: 366
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: CreateFormData - Creates "multipart/form-data" for http post

17 Jan 2016, 22:54

Thank you for this, tmplinshi! I've been searching for hours to find a way to upload a text file to my server and was almost giving up. Very nice! ;)
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
Suresh
Posts: 35
Joined: 03 May 2016, 18:58

Re: CreateFormData - Creates "multipart/form-data" for http post

03 May 2016, 19:25

@tmplinshi

Wonderful code! Thank you very much for sharing this.
I've adapted your code into a standalone function for my flickr class.

I have one doubt. The following part of code will accept multiple images and post them under the same parameter.

Code: [Select all] [Download] GeSHi © Codebox Plus

If IsObject(v) {
For i, FileName in v


Is this loop intended? Wouldn't FileName := v[1] be sufficient?

Thanks again.
tmplinshi
Posts: 1090
Joined: 01 Oct 2013, 14:57

Re: CreateFormData - Creates "multipart/form-data" for http post

04 May 2016, 00:39

@Suresh

Yes, it was intended to support multiple files, e.g. "upload[]" : ["1.png", "2.png"]
I've added description and example to the #1 post.
Suresh
Posts: 35
Joined: 03 May 2016, 18:58

Re: CreateFormData - Creates "multipart/form-data" for http post

04 May 2016, 05:51

Fantastic! Thanks for the clarification and the updated example.
Now I need to check if flickr supports multiple images per POST.
carno
Posts: 82
Joined: 20 Jun 2014, 16:48

Re: CreateFormData - Creates "multipart/form-data" for http post

06 May 2016, 08:31

Could you please explain in newbie terms what this code does?
tmplinshi
Posts: 1090
Joined: 01 Oct 2013, 14:57

Re: CreateFormData - Creates "multipart/form-data" for http post

06 May 2016, 09:23

@carno

If you want to post string "key=val" using WinHttp or XMLHttp, it's easy:

Code: [Select all] [Download] GeSHi © Codebox Plus

whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("POST", URL)
whr.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
whr.Send("key=val")


But if you want to post a binary file, such as uploading an image, there's no direct way like whr.Send("file=c:\test.png"). CreateFormData() can be used to create the PostData, which can be used by whr.Send(PostData).
Suresh
Posts: 35
Joined: 03 May 2016, 18:58

Re: CreateFormData - Creates "multipart/form-data" for http post

06 May 2016, 10:06

@carno

Take a look at flickr example:
https://www.flickr.com/services/api/upload.example.html

You will see {RAW JFIF DATA} at the end of that POST, which in actual should be the image file contents in binary....
whereas the rest of the POST has to be in UTF-8.

CreateFormData() accepts parameters and creates the HTTP POST mixing UTF-8 text and binary together.

Hope that helps.
Suresh
Posts: 35
Joined: 03 May 2016, 18:58

Re: CreateFormData - Creates "multipart/form-data" for http post

08 May 2016, 21:06

Ref: ByteArray() by Coco
Here is an alternate version that builds formdata ( UTF-8 + Binary ) on Global memory and then copies it to a COM bytearray.

Spoiler


Two copies of formdata exists on memory which was what Coco was trying to avoid with his ByteArray() function.
The simpler, straight forward, memory conservative method would be to build formdata as a file object and then load it to a COM bytearray.
tmplinshi
Posts: 1090
Joined: 01 Oct 2013, 14:57

Re: CreateFormData - Creates "multipart/form-data" for http post

08 May 2016, 22:34

Thank you Suresh! I did tried the ByteArray() before, but I can't figure out how to use it to combine string and binary data. The code you provide works, thanks.
Bruttosozialprodukt
Posts: 451
Joined: 24 Jan 2014, 22:28

Re: CreateFormData - Creates "multipart/form-data" for http post

11 May 2016, 02:27

Did you actually get the winhttprequest object to send binary files? Or only text-based files?
I've done something similar before, but I was not able to do it with the internal com object.
https://autohotkey.com/boards/viewtopic ... 986#p21986
tmplinshi
Posts: 1090
Joined: 01 Oct 2013, 14:57

Re: CreateFormData - Creates "multipart/form-data" for http post

11 May 2016, 09:36

Bruttosozialprodukt wrote:Did you actually get the winhttprequest object to send binary files?

Yes, as you can see in the first post example or SKAN's modified version or https://autohotkey.com/boards/viewtopic ... 731#p41731.
Bruttosozialprodukt
Posts: 451
Joined: 24 Jan 2014, 22:28

Re: CreateFormData - Creates "multipart/form-data" for http post

11 May 2016, 12:55

That's sick, I never thought of this approach.
Doode

Re: CreateFormData - Creates "multipart/form-data" for http post

07 Jun 2016, 07:26

Is it possible to make CreateFormData work with VxE's HttpRequest ?
The reason I need this is because it's a very simple matter to show upload progress with HTTPRequest. But it's apparently impossible to show upload progress with this COM WinHttpRequest.5.1 used in your example. I've tried to use the postdata that CreateFormData makes with HttpRequest, but it somehow gets garbled when it's sent to the sever and isn't interpreted correctly. Does anyone know why this is happening?
tmplinshi
Posts: 1090
Joined: 01 Oct 2013, 14:57

Re: CreateFormData - Creates "multipart/form-data" for http post

17 Jun 2016, 03:25

Update on version 1.20 / 2016-6-17 - Added CreateFormData_WinInet(), which can be used for VxE's HTTPRequest().

Hi Doode, use CreateFormData_WinInet().
arcticir
Posts: 471
Joined: 17 Nov 2013, 11:32

Re: CreateFormData - Creates "multipart/form-data" for http post

14 Aug 2016, 22:02

V2 HTTPRequest()

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

DanielToward13
Posts: 35
Joined: 18 May 2017, 10:56

Re: CreateFormData - Creates "multipart/form-data" for http post

30 May 2017, 15:14

I need to upload a file (photo, audio, etc) to my API using multipart/form-data. I am using your code and it uploads the photo successfully but at the end I get the below error:


What are the settings that needs to be changed? How can I get the response from my API after uploading the file? (The response should return in JSON)

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by DanielToward13 on 31 May 2017, 03:19, edited 2 times in total.
tmplinshi
Posts: 1090
Joined: 01 Oct 2013, 14:57

Re: CreateFormData - Creates "multipart/form-data" for http post

30 May 2017, 22:22

That means there is no Location header in the response headers. So just remove that line.
To retrieve the response content, use whr.ResponseText, e.g. MsgBox % whr.ResponseText.
User avatar
Joe Glines
Posts: 465
Joined: 30 Sep 2013, 20:49
Facebook: https://www.facebook.com/theAutomatorGuru/
Google: https://plus.google.com/105328929654286634910
GitHub: joetazz
Location: Dallas
Contact:

Re: CreateFormData - Creates "multipart/form-data" for http post

20 Jun 2017, 13:05

BTW- I'd seen this before (and even used it) but I was unclear on what "multipart/form-data" meant and why I would need to use it.

I found this post on Stackoverflow which gives a pretty good explanation. :)
egocarib
Posts: 86
Joined: 21 May 2015, 18:21

Re: CreateFormData - Creates "multipart/form-data" for http post

15 Oct 2017, 12:42

Anyone know what I need to do to get this to work in AHKv2? (64-bit)

Specifically, I am trying to use this to send a file over HTTP, based on BlackHolyMan's example in this thread.

Running into this error (in BinArr.ahk)

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus




I spent some time debugging but can't really figure out the problem. The BinArr_ functions seem to be working okay but I don't fully understand it (the ADO stream is working correctly I think: I can read size, position, etc, and it returns an object, which I assume is a binary object)

Seems like BinArr also causes problems for some other people. See here and here. Not sure if they had the same problem. Maybe I should try the alternative they suggest?


My code (BlackHolyMan's code with double-quote escaping updated to work for AHKv2)

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



CreateFormData.ahk (only updated double-quote escaping for AHKv2 compatibility)

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



BinArr.ahk (unchanged)

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus





(Working on a general PushBullet class for AHKv2 but the file sending here is the last wrench holding me up from supporting the core elements of the PushBullet API)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 19 guests