Jump to content
Sign in to follow this  
judas

Help with POSTing image to imgur [winhttp]

Recommended Posts

judas

I've been trying to uplaod a picture to imgur.com with functions from winhttp.au3

Examples using curl use the -F switch to POST form data. So I tried the same with _WinHttpSimpleFormFill to imgur.com/api/upload.xml.

..and @error is set to 1. No form on that page? The what does curl do :huh2: I sure am missing something. Help a bit?

Global $hOpen = _WinHttpOpen(" Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5) ")

; Get connection handle
Global $hConnect = _WinHttpConnect( $hOpen, "imgur.com" )

Global $sHTM = _WinHttpSimpleFormFill( $hConnect, "api/upload.xml", "index:0", "name:key", "b3625162d3418ac51a9ee805b1840452", "name:image", _PathFull($tempfile) )

If @error Then MsgBox(68, "", "Error" & @error)

ConsoleWrite( $sHTM )
; Close handles
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

Share this post


Link to post
Share on other sites
trancexx

There are no forms there, the output you get from form filling function is correct.

What you should do if you want to use _WinHttpSimpleFormFill() is to create form for your self. Like this:

#include "WinHTTP.au3"

$sFile = @ScriptDir & "\testimage.jpg"

$sForm = _
        '<form action="http://imgur.com/api/upload.xml" method="post" enctype="multipart/form-data">' & _
        '    <input type="file" name="image" />' & _
        '    <input name="key">' & _
        '</form>'

; Initialize and get session handle
$hOpen = _WinHttpOpen()

; Form is string initially
$hForm = $sForm

; Fill form on this page
$sRead = _WinHttpSimpleFormFill($hForm, $hOpen, Default, "name:image", $sFile, "name:key", "b3625162d3418ac51a9ee805b1840452")

; Close connection handle
_WinHttpCloseHandle($hForm)
; Close session handle
_WinHttpCloseHandle($hOpen)

; See what's returned
If $sRead Then ConsoleWrite($sRead & @CRLF)

That script will upload testimage.jpg and will print to console all the info you would need.

  • Like 2

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
judas

This Works.

But, why is this needed ?

$hForm = $sForm

Anyway, thanks lot :thumbsup: for both the UDF you provide and your kind help.

Edited by judas

Share this post


Link to post
Share on other sites
FireFox

@judas

trancexx trys to keep as more as possible the right type of variables.

The first argument for the function _WinHttpSimpleFormFill is $hInternet which seems to be a handle (according to the first letter h), but if you read the parameter description you will notice that it can also be a "string variable with form.".

You can directly do this :

$sForm = '<form action' ...

$sRead = _WinHttpSimpleFormFill($sForm, ...

But it's neater to let it as he gave you.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
trancexx

In some serious code $sForm would likely be declared as constant. As you can see I used implicit type of declaration by assignment for brevity, leaving the AutoIt to choose advertised default scope, extent and any other possible variable property.

_WinHttpSimpleFormFill() takes first argument Byref, therefore it's not correct to pass constant there.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
water

But it's neater to let it as he gave you.

Correct is: ... as she gave you. ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
FireFox

Correct is: ... as she gave you. ;)

Ops, my apologizes to her then.

@trancexx

Thank you for your deeper explanation.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×