Jump to content

Recommended Posts

Posted

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)
Posted

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.

♡♡♡

.

eMyvnE

Posted (edited)

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
Posted (edited)

@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
Posted

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

Posted

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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

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

Ops, my apologizes to her then.

@trancexx

Thank you for your deeper explanation.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...