Jump to content
trancexx

WinHTTP functions

Recommended Posts

It could be something like this:

#include <WinHttp.au3>

$url = "https://slack.com/api/files.upload"
$token = "abc-123-456-abc123"
$args = "#random"
$file = "c:\Users\Public\catloloz.jpg"

; example curl command -- curl -F file=@c:\Users\Public\catloloz.jpg -F channels=#random -F token=abc-123-456-abc123 https://slack.com/api/files.upload


$sForm = _
        '<form action="' & $url & '" method="post" enctype="multipart/form-data">' & _
        ' <input type="file" name="file"/>' & _ ;
        ' <input name="channels" />' & _
        ' <input name="token" />' & _
        '</form>'

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

$hConnect = $sForm ; will pass form as string so this is for coding correctness because $hConnect goes in byref

; Fill form
$sHTML = _WinHttpSimpleFormFill($hConnect, $hOpen, _
        Default, _
        "name:file", $file, _
        "name:channels", $args, _
        "name:token", $token)

If @error Then
    MsgBox(4096, "Error", "Error number = " & @error)
Else
    ConsoleWrite("!" & $sHTML & @CRLF)
EndIf

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

 


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
1 hour ago, samibb said:

WHEN I RUN EXAMPLE AT HOME WAS WORKING. BUT ON NETWORK NOT WORKING.

IS THERE ANY SETTING BE4 USING WINHTTP?

@trancexx : Background information :)

To understand what this question is about at all, see https://www.autoitscript.com/forum/topic/203849-winhttp-invalid-server-response/?do=findComment&comment=1471590


Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share this post


Link to post
Share on other sites

Hello,

i'm trying to use the WinHttp functions to read and write to a relay board. i have no problem for reading data with GET Method. But i can't find how to write something using the PUT method as asked in the documentation.

According to the restfulAPI documentation of the board ( https://www.moxa.com/getmedia/6b024adb-5276-429d-9837-1235ee4ac836/moxa-iologik-e1200-series-manual-v15.9.pdf )

jtxAtoI.png

sxvUK3R.png

So i haveto use the PUT method, is that working with WinHttp ?

this is actualy how i read something :

#include "json.au3"
        #include "WinHttp.au3"
        $hOpen = _WinHttpOpen() ; Initialize and get session handle
        $hConnect = _WinHttpConnect($hOpen, "192.168.8.250") ; Get connection handle

        Local $hRequest = _WinHttpOpenRequest($hConnect, Default, "/api/slot/0/io/relay")
        _WinHttpAddRequestHeaders($hRequest, "Content-Type: application/json")
        _WinHttpAddRequestHeaders($hRequest, "Accept: vdn.dac.v1")
        _WinHttpSendRequest($hRequest)
        _WinHttpReceiveResponse($hRequest)
        $data = _WinHttpReadData($hRequest)

        Local $sHeader = _WinHttpQueryHeaders($hRequest)
        ;MsgBox(0, "Header", $sHeader)

        _WinHttpCloseHandle($hRequest)
        _WinHttpCloseHandle($hConnect)
        _WinHttpCloseHandle($hOpen)

        $object = json_decode($data)
        $relayCurrentCount = json_get($object, '[io][relay][4][relayCurrentCount]')

        msgbox ("","Result", "relayCurrentCount = " & $relayCurrentCount )

 

And below my PHP code, using cURL to write something with a PUT method :

<?php

$ch = curl_init("http://192.168.8.250/api/slot/0/io/relay/0/relayStatus"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: vdn.dac.v1'
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, '$sPost = '{"slot":0,"io":{"relay":{"0":{"relayStatus":1}}}}');
$result = curl_exec($ch);
curl_close($ch);

echo $result;

?>

So my question is, how can i do the same with WinHTTP ??

Thank you for helping.

Edited by cetipabo
add pdf link

Share this post


Link to post
Share on other sites

Finaly i found something that works, but i don't know if this is the correct way to add the custom headers ??

$hOpen = _WinHttpOpen()
        $hConnect = _WinHttpConnect($hOpen, '192.168.8.250')

        $sPost = '{"slot":0,"io":{"relay":{"0":{"relayStatus":1}}}}'

        $sResult = _WinHttpSimpleRequest($hConnect, "PUT", '/api/slot/0/io/relay/0/relayStatus', '', $sPost, "Accept: vdn.dac.v1" & @CRLF & "Content-Type: application/json")
        _WinHttpCloseHandle($hConnect)
        _WinHttpCloseHandle($hOpen)

 

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...