Convert cURL to AHK for TVDB API Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
starionx
Posts: 17
Joined: 23 Jul 2015, 22:29

Convert cURL to AHK for TVDB API

06 May 2020, 03:50

I know nothing about cURL, and almost nothing about Objects. However, I have managed to convert a GET command by perusing various posts -

curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer -ridiculouslylongtokenkey-' 'https://api.thetvdb.com/search/series?name=gilmore%20girls'

TVDBSearchName = gilmore girls
WinHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
GetParm1 := "https://api.thetvdb.com/search/series?name=" . TVDBSearchName
WinHTTP.Open("GET", GetParm1, false)
WinHTTP.SetRequestHeader("Accept", "application/json")
;WinHTTP.SetRequestHeader("Accept", "application/vnd.thetvdb.v1")
WinHTTP.SetRequestHeader("Authorization", "Bearer -ridiculouslylongtokenkey-")
WinHTTP.Send()


but getting the initial token key is beyond my abilities. The cURL code -

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
"apikey": "insertkeyhere",
"userkey": "insertuserkeyhere",
"username": "insertusernamehere"
}' 'https://api.thetvdb.com/login'

(one of my pitiful attempts)

WinHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHTTP.Open("POST", "https://api.thetvdb.com/login", false)
WinHTTP.SetRequestHeader("Content-Type", "application/json")
WinHTTP.SetRequestHeader("Accept", "application/json")
Body = "{"apikey": "insertkeyhere","userkey": "insertuserkeyhere","username": "insertusernamehere"} "https://api.thetvdb.com/login"
WinHTTP.Send(Body)

Response := WinHTTP.ResponseText
ApiStatus := WinHTTP.Status
msgbox %Response%


Everything I've tried either gives me script errors or API auth errors.
Help! :cry:

edit - can someone explain how to get rid of the 'Broken Link for safety' messages?

[Mod: edited links in code boxes]
BNOLI
Posts: 548
Joined: 23 Mar 2020, 03:55

Re: Convert cURL to AHK for TVDB API

06 May 2020, 07:30

edit - can someone explain how to get rid of the 'Broken Link for safety' messages?
With only 7 threads on your counter you haven't already 'qualified' to be off the hook -regarding the forum's security/anti-spam filters. If you're a lucky one, a moderator will fix them manually on your behalf, as long as you won't edit that posting, so they will break again :shh:
Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !
starionx
Posts: 17
Joined: 23 Jul 2015, 22:29

Re: Convert cURL to AHK for TVDB API

06 May 2020, 07:44

I kind of thought that may be the case, although... 'Joined:24 Jul 2015, 13:29', you would think that if I was going to do something nasty, I probably would have done it by now. :lol:
BNOLI
Posts: 548
Joined: 23 Mar 2020, 03:55

Re: Convert cURL to AHK for TVDB API

06 May 2020, 10:03

Let's see it that way, staying that long in stealth mode is making you even more suspicious ... :shh: :lol:
Btw, why not working with a "pathVar := C:\..." setting, so you won't have to change multiple instances at different positions in your script if necessary? ;)
Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !
starionx
Posts: 17
Joined: 23 Jul 2015, 22:29

Re: Convert cURL to AHK for TVDB API

06 May 2020, 10:56

:shh: you're starting to blow my cover. Just a few more years in the shadows, then... BOOM!!!

As for the pathVars... I'm just trying to get things to work first, then I'll make it pretty. ;)
teadrinker
Posts: 4400
Joined: 29 Mar 2015, 09:41
Contact:

Re: Convert cURL to AHK for TVDB API  Topic is solved

06 May 2020, 11:18

Try this:

Code: Select all

WinHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHTTP.Open("POST", "https://api.thetvdb.com/login", false)
WinHTTP.SetRequestHeader("Content-Type", "application/json")
WinHTTP.SetRequestHeader("Accept", "application/json")
Body = {"apikey":"insertkeyhere","userkey":"insertuserkeyhere","username":"insertusernamehere"}
WinHTTP.Send(Body)

Response := WinHTTP.ResponseText
ApiStatus := WinHTTP.Status
msgbox %Response%
starionx
Posts: 17
Joined: 23 Jul 2015, 22:29

Re: Convert cURL to AHK for TVDB API

06 May 2020, 19:54

WooHoo!!! Thanks a lot teadrinker. Worked like a charm. Obviously, cURL didn't like the space I added after the semi-colons in 'Body'.

:dance:
marceldeeno
Posts: 9
Joined: 03 Jun 2022, 03:13

Re: Convert cURL to AHK for TVDB API

07 Apr 2023, 09:52

Thanks to everyone working on this script. I have made an API to upload an image and that works fine with Postman, see print screen. Now I'm also trying this with autohotkey but I can't get it done. Can someone see what I'm doing wrong here?

Code: Select all

WinHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHTTP.Open("POST", "https://tst.webcentre.nl/rest-api/items/upload1.php", false)
WinHTTP.SetRequestHeader("Content-Type", "application/json")
WinHTTP.SetRequestHeader("Accept", "application/json")
Body = {"sendimage":"e:\image.jpg"}
WinHTTP.Send(Body)

Response := WinHTTP.ResponseText
ApiStatus := WinHTTP.Status
msgbox %Response%
Image

Image
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Convert cURL to AHK for TVDB API

07 Apr 2023, 10:56

in postman ure sending multipart formdata. in ahk ure sending json. clearly the api endpoint doesnt expect to receive json. u need to send multipart formdata instead
marceldeeno
Posts: 9
Joined: 03 Jun 2022, 03:13

Re: Convert cURL to AHK for TVDB API

08 Apr 2023, 08:18

Okay, now I've changed some things but it still doesn't work. I have also tried other script like but also not working. What could this be?

Code: Select all

WinHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHTTP.Open("POST", "https://tst.webcentre.nl/rest-api/items/upload1.php", false)
WinHTTP.SetRequestHeader("Content-Type", "multipart/form-data")
WinHTTP.SetRequestHeader("Accept", "multipart/form-data")
Body = {"sendimage":"e:\image.jpg"}
WinHTTP.Send(Body)
Response := WinHTTP.ResponseText
ApiStatus := WinHTTP.Status
msgbox %Response%
Uploading with postman works fine

Code: Select all

<?php

header("Content-Type: application/json");
header("Acess-Control-Allow-Origin: *");
header("Acess-Control-Allow-Methods: POST");
header("Acess-Control-Allow-Headers: Acess-Control-Allow-Headers,Content-Type,Acess-Control-Allow-Methods, Authorization");

include 'dbconfig.php'; // include database connection file

$data = json_decode(file_get_contents("php://input"), true); // collect input parameters and convert into readable format

if(empty($fileName))
{
	$errorMSG = json_encode(array("message" => "please select image", "status" => false));	
	echo $errorMSG;
}
else
{
	$upload_path = 'upload/'; // set upload folder path 
	
	$fileExt = strtolower(pathinfo($fileName,PATHINFO_EXTENSION)); // get image extension
		
	// valid image extensions
	$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); 
					
	// allow valid image file formats
	if(in_array($fileExt, $valid_extensions))
	{				
		//check file not exist our upload folder path
		if(!file_exists($upload_path . $fileName))
		{
			// check file size '5MB'
			if($fileSize < 5000000){
				move_uploaded_file($tempPath, $upload_path . $fileName); // move file from system temporary path to our upload folder path 
			}
			else{		
				$errorMSG = json_encode(array("message" => "Sorry, your file is too large, please upload 5 MB size", "status" => false));	
				echo $errorMSG;
			}
		}
		else
		{		
			$errorMSG = json_encode(array("message" => "Sorry, file already exists check upload folder", "status" => false));	
			echo $errorMSG;
		}
	}
	else
	{		
		$errorMSG = json_encode(array("message" => "Sorry, only JPG, JPEG, PNG & GIF files are allowed", "status" => false));	
		echo $errorMSG;		
	}
}
		
// if no error caused, continue ....
if(!isset($errorMSG))
{
	$query = mysqli_query($conn,'INSERT into tbl_image (name) VALUES("'.$fileName.'")');
			
	echo json_encode(array("message" => "Image Uploaded Successfully", "status" => true));	
}

?>
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Convert cURL to AHK for TVDB API

12 Apr 2023, 07:48

i dont know what ure trying to change man...
uve changed the Content-Type header - OK.
uve also changed the Accept header to multipart/form-data - why? the api isnt sending its response as multipart/form-data
ure still sending a json-like string in the request's body
ure not sending multipart/form-data in the request's body
if u dont know how to construct multipart/form-data, read https://www.rfc-editor.org/rfc/rfc2046#section-4.5.1
marceldeeno
Posts: 9
Joined: 03 Jun 2022, 03:13

Re: Convert cURL to AHK for TVDB API

14 Apr 2023, 09:57

Thank you for your response, I have studied it a bit better and after the lesson with autohotkey together with fiddler it becomes much clearer. In the meantime I have come quite a bit further, he also uploads files correctly now, but the image that is sent with it is 0kb. somewhere along the way something goes wrong. I also checked a few things with fiddler, but as far as I can see everything is fine. Maybe someone can help me with this? I'm willing to pay for it, no problem, I'm not such a big programmer. thank you

Code: Select all

File := "1.jpg"
Boundary := rBoundary()
wh := ComObjCreate("WinHttp.WinHttpRequest.5.1")
wh.SetProxy(2,"localhost:8866") ;Send data through Fiddler
wh.Open("Post", "https://tst.webcentre.nl/api/api-file-upload.php")
wh.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36")
wh.SetRequestHeader("Accept", "*/*")
wh.SetRequestHeader("Accept-Encoding", "gzip, deflate, br")
wh.SetRequestHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary" Boundary)



B := "------WebKitFormBoundary" Boundary

v2 := "Content-Disposition: form-data; name=""file""; filename=""" file """"
v3 := "Content-Type: image/jpg"

FileRead, v, % File

CRLF := "`r`n"

FormData := B CRLF CRLF 0 CRLF B CRLF v2 CRLF v3 CRLF CRLF v CRLF B "--"

wh.Send(FormData)
Msgbox, , Form-Data, % FormData
status := wh.ResponseText

Msgbox, , % status, % res1

rBoundary() {
	str := "0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z"
	Sort, str, D| Random
	str := StrReplace(str, "|")
	Return SubStr(str, 1, 16)
}
marceldeeno
Posts: 9
Joined: 03 Jun 2022, 03:13

Re: Convert cURL to AHK for TVDB API

14 Apr 2023, 10:14

Image
marceldeeno wrote:
14 Apr 2023, 09:57
Thank you for your response, I have studied it a bit better and after the lesson with autohotkey together with fiddler it becomes much clearer. In the meantime I have come quite a bit further, he also uploads files correctly now, but the image that is sent with it is 0kb. somewhere along the way something goes wrong. I also checked a few things with fiddler, but as far as I can see everything is fine. Maybe someone can help me with this? I'm willing to pay for it, no problem, I'm not such a big programmer. thank you

Code: Select all

File := "1.jpg"
Boundary := rBoundary()
wh := ComObjCreate("WinHttp.WinHttpRequest.5.1")
wh.SetProxy(2,"localhost:8866") ;Send data through Fiddler
wh.Open("Post", "https://tst.webcentre.nl/api/api-file-upload.php")
wh.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36")
wh.SetRequestHeader("Accept", "*/*")
wh.SetRequestHeader("Accept-Encoding", "gzip, deflate, br")
wh.SetRequestHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary" Boundary)



B := "------WebKitFormBoundary" Boundary

v2 := "Content-Disposition: form-data; name=""file""; filename=""" file """"
v3 := "Content-Type: image/jpg"

FileRead, v, % File

CRLF := "`r`n"

FormData := B CRLF CRLF 0 CRLF B CRLF v2 CRLF v3 CRLF CRLF v CRLF B "--"

wh.Send(FormData)
Msgbox, , Form-Data, % FormData
status := wh.ResponseText

Msgbox, , % status, % res1

rBoundary() {
	str := "0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z"
	Sort, str, D| Random
	str := StrReplace(str, "|")
	Return SubStr(str, 1, 16)
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Google [Bot] and 206 guests