Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Post request with MultiPart

Post Reply
gresaggr
Posts: 22
Joined: Mon Mar 04, 2019 12:41 pm

Post request with MultiPart

Post by gresaggr »

Hello.
How can I send request with multipart/form-data with image file?
For example:

Content-Disposition: form-data; name="model"
news_image


Content-Disposition: form-data; name="img"; filename="1.jpg"
Content-Type: image/jpeg



P.S. Or probably is it possible like in Selenium - SendKeys to element with full file name?
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Post request with MultiPart

Post by salvadordf »

Hi,

You should be able to do both things.

You can create a custom POST request using the code in the URLRequest demo. You would have to encode the image file and call TCefPostDataElementRef.SetToBytes. You may also have to add a "content-type" header to the request.

Alternatively, you can also emulate keyboard and mouse events to make the browser send the file for you. You can use a browser in OSR mode to log all the mouse and keyboard events, and replicate them later. Use the SimpleOSRBrowser demo for that.
gresaggr
Posts: 22
Joined: Mon Mar 04, 2019 12:41 pm

Re: Post request with MultiPart

Post by gresaggr »

Ok. Thank you.
igor666
Posts: 71
Joined: Fri Feb 08, 2019 1:25 pm

Re: Post request with MultiPart

Post by igor666 »

Hi. Faced the same problem, unable to send multipart/form-data request. I don’t understand how to make the request body as follows:

Code: Select all

Header
...
Content-Type: multipart/form-data; boundary=---------------573cf973d5228--
...

Body
---------------573cf973d5228
Content-Disposition: form-data; name="upload"

Загрузить
---------------573cf973d5228
Content-Disposition: form-data; name="myfile"; filename="my_file_name.jpg"
Content-Type: image/jpeg

Content file
---------------573cf973d5228--
How to insert the title is clear, but how the request forms the body, I cannot understand. I tried to do it as described in the URLRequest demo, but the server does not accept such a request. And in this demo, it is not clear how the same boundary is formed and where it is inserted. I need to send a file to the server in the body of a multipart/form-data request, but unfortunately nothing works. Could you explain in more detail how this can be done. Thank you in advance.
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Post request with MultiPart

Post by salvadordf »

I've never done that but I can give you the links to the information that might help you.

The URLRequest demo uses these CEF classes to send the POST request :
  • TCefRequestRef : https://magpcss.org/ceforum/apidocs3/projects/(default)/CefRequest.html
  • TCefPostDataRef : https://magpcss.org/ceforum/apidocs3/projects/(default)/CefPostData.html
  • TCefPostDataElementRef : https://magpcss.org/ceforum/apidocs3/projects/(default)/CefPostDataElement.html
The official forum has some threads that might be useful too :
https://www.magpcss.org/ceforum/viewtopic.php?f=6&t=396
igor666
Posts: 71
Joined: Fri Feb 08, 2019 1:25 pm

Re: Post request with MultiPart

Post by igor666 »

OK, thanks for the info.
Post Reply