Jump to content
Sign in to follow this  
karlkar

WinHttp - posting a file

Recommended Posts

karlkar

Hello.

Can you help me in sending a file with WinHttp? When I execute 

curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@myfile.txt" http://myhost/rest/api/2/issue/TEST-123/attachments

it works. My tries of implementing it in AutoIt failed. Can you help?

Share this post


Link to post
Share on other sites
karlkar

Of course.

Here you are one, the last of them

#include "../WinHttp.au3"

Local $hOpen = _WinHttpOpen()
Local $hConnect = _WinHttpConnect($hOpen, "myhost")

Local $hRequest = _WinHttpOpenRequest($hConnect, "POST", "rest/api/2/issue/TEST-123/attachments")
_WinHttpSetCredentials($hRequest, $WINHTTP_AUTH_TARGET_SERVER, $WINHTTP_AUTH_SCHEME_BASIC, "admin", "admin")

; getting response from server and pasting it to clipboard
Local $boundary = @YEAR & @MON & @MDAY

Local $sData = 'Content-Disposition: form-data; name="file"; filename="myfile.txt"' & @CRLF
$sData &= 'Content-Type: application/raw' & @CRLF & @CRLF
$sData &= FileRead("C:/PLM2CAM/myfile.txt") & @CRLF
$sData &= '-----------------------------' & $boundary & @CRLF
_WinHttpSendRequest($hRequest, "Content-Type: multipart/form-data; X-Atlassian-Token: nocheck; boundary=---------------------------" & $boundary & @CRLF, $sData)
_WinHttpReceiveResponse($hRequest)
Local $sHeader = _WinHttpReadData($hRequest)
ClipPut($sHeader)
MsgBox(0, "", $sHeader)

Response I get is:

 

Cause:
javax.ws.rs.WebApplicationException: java.lang.IllegalArgumentException: Error parsing media type 'multipart/form-data; X-Atlassian-Token: nocheck; boundary=---------------------------20130902'

Stack Trace:

javax.ws.rs.WebApplicationException: java.lang.IllegalArgumentException: Error parsing media type 'multipart/form-data; X-Atlassian-Token: nocheck; boundary=---------------------------20130902'
    at com.sun.jersey.server.impl.model.HttpHelper.clientError(HttpHelper.java:265)
    at com.sun.jersey.server.impl.model.HttpHelper.getContentType(HttpHelper.java:91)

Share this post


Link to post
Share on other sites
trancexx

Based on your code, something like this could work:

#include "../WinHttp.au3"

Global Const $sFileToUpload = "C:/PLM2CAM/myfile.txt" ;<- Set correct file location here
Global Const $sAddress = "http://myhost/rest/api/2/issue/TEST-123/attachments" ;<- Set correct host here

; Form to fill (just in case API doesn't have transparent form
Local $sForm = _
        '<form action="' & $sAddress & '" method="post" enctype="multipart/form-data">' & _
        '    <input type="file" name="file"/>' & _ ;
        '</form>'

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

$hConnect = $sForm ; will pass form as string so this swap is for coding correctness because $hConnect goes in byref
; Fill form
$sHTML = _WinHttpSimpleFormFill($hConnect, $hOpen, Default, "name:file", $sFileToUpload, "X-Atlassian-Token: nocheck")

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

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

♡♡♡

.

eMyvnE

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  

×