Topic: How To use "WinHttp.WinHttpRequest.5.1" API Form Upload

In Postman test API post method Line Notify
text message and upload image
result

{"status":200,"message":"ok"}

https://i.ibb.co/425Hw9S/D20230602-T094551.png

Postman Code HTTP

POST /api/notify HTTP/1.1
Host: notify-api.line.me
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Authorization: Bearer 123456789
Content-Length: 296

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="message"

test
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="imageFile"; filename="/C:/Pic/D20230524T092936.png"
Content-Type: image/png

(data)
------WebKitFormBoundary7MA4YWxkTrZu0gW--

Postman Code curl

curl --location '[url]https://notify-api.line.me/api/notify[/url]' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Authorization: Bearer 123456789' \
--form 'message="test"' \
--form 'imageFile=@"/C:/Pic/D20230524T092936.png"'



IN MVD  I'm Try use method  "WinHttp.WinHttpRequest.5.1"  Post And Upload Image File
IF Sendy Message Only It "OK"
but
IF Send Message + Upload Image Result Error
How To Edit Script ? Help Me Please

MyScript In MVD

var
WinHttpReq : Variant;
procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
url : String;
imageFile : String ;
token : String ;
body : Tstringlist ;
begin
body := Tstringlist.create ;
    token := '123456789' ;
    imageFile := 'imageFile=@"/C:/Pic/D20230524T092936.png"';
    url := 'https://notify-api.line.me/api/notify' ;
    WinHttpReq.Open('POST',url, False);
    WinHttpReq.SetRequestHeader('ContentEncoding','UTF-8');
    WinHttpReq.SetRequestHeader('Content-Type','application/x-www-form-urlencoded') ;
    WinHttpReq.SetRequestHeader('Authorization','Bearer '+token) ;
    Try
        WinHttpReq.send('message="test"'+'\'+'imageFile=@"/C:/Pic/D20230524T092936.png"');
        ShowMessage(WinHttpReq.ResponseText);
        Except
        Showmessage(ExceptionMessage) ;
    End;
body.Free ;
end;
begin
    WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
end.

In

My Visual Database : I Love You
Easy For Beginner Student For Me

2 (edited by sparrow 2023-06-02 10:35:30)

Re: How To use "WinHttp.WinHttpRequest.5.1" API Form Upload

Hi,


If I understood you correctly, you are talking about parsing the response from the server.

Here's what you can get (Example):
"WinHttpReq.Status" - 401
"WinHttpReq.ResponseText" - {"status":401,"message":"Invalid access token"}


In this case, you can do the following.
1. You can get a clean SEND response code WinHttpReq.Status (Number).
Analyzing it, you will understand that it is OK (200) or it is an error (for example 401) and if you need the server error text,
you parse the WinHttpReq.ResponseText response - JSON {"status":401,"message":"Invalid access token"} .
Alternatively, create your own error text in the program and bind it to the WinHttpReq.Status code
without parsing the WinHttpReq.ResponseText server response.


OR


2.After the response, you need to parse WinHttpReq.ResponseText - JSON {"status":401,"message":"Invalid access token"}
and make further decisions based on this.


Judging by the manual, there are not so many codes.
You can find the response codes on the website in the LINE Notify API Document section.


Parsing example here http://myvisualdatabase.com/forum/viewt … p?id=8534.
Your server response is simpler.

OK Code example Full parse

var
  url : String;
  imageFile : String ;
  token : String ;
  body : Tstringlist ;
  jDataStream: TJSONObject;
begin

. . . 
   
Try
       WinHttpReq.send('message="test"'+'\'+'imageFile=@"/C:/Pic/D20230524T092936.png"');

       jDataStream := TJSONObject(TJSONObject.ParseJSONValue(WinHttpReq.ResponseText));

       if (jDataStream <> nil) then
       begin
         showmessage(TJSONValue(jDataStream.GetPairByName('status').JsonValue).Tostring);
         showmessage(TJSONValue(jDataStream.GetPairByName('message').JsonValue).Tostring);
       end else showmessage('Something is wrong');

. . . 

Re: How To use "WinHttp.WinHttpRequest.5.1" API Form Upload

hi sparrow
1. Try use Postman result  OK Send message and image Correct

{"status":200,"message":"ok"}

https://i.ibb.co/k1snVt9/D20230607-T094225.png

2. Try use MVD use WinHttp.WinHttpRequest.5.1 + multipart/form-data; boundary=--AAA + message OK Send message Correct

{"status":200,"message":"ok"}

https://i.ibb.co/HXmBJdX/D20230607-T094733.png
mvd code

var
WinHttpReq : Variant;
procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
url  : String ;
token : String ;
FileName : String;
begin
    FileName := 'C:\Pic\pas.png' ;
    token := 'xxxxxxxxxx' ;
    WinHttpReq.Open('POST','https://notify-api.line.me/api/notify', false);
    WinHttpReq.SetRequestHeader('Authorization','Bearer '+token) ;
    WinHttpReq.SetRequestHeader('Content-Type', 'multipart/form-data; boundary=--AAA');
    Try
        WinHttpReq.Send(
        '----AAA'+#13#10+
        'Content-Disposition: form-data; name="message"'+#13#10+#13#10+
        #13#10+'['+FormatDateTime('yyyy-mm-dd hh:nn:zz', Now)+' : Test-MVD-API'+']'+#13#10+
        #13#10+'----AAA--'+#13#10);
        Form1.Memo1.Lines.Add( WinHttpReq.Responsetext );
        Except
        Showmessage(ExceptionMessage) ;
    End;
end;
begin
    WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
end.

3. Try use MVD use WinHttp.WinHttpRequest.5.1 + multipart/form-data; boundary=--AAA + message + image  = Error
Help me Please ?

{"status":400,"message":"Invalid image."}

mvd code

var
WinHttpReq : Variant;
procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
url  : String ;
token : String ;
FileName : String;
begin
    FileName := 'C:\Pic\pas.png' ;
    token := 'xxxxxxxxxx' ;
    WinHttpReq.Open('POST','https://notify-api.line.me/api/notify', false);
    WinHttpReq.SetRequestHeader('Authorization','Bearer '+token) ;
    WinHttpReq.SetRequestHeader('Content-Type', 'multipart/form-data; boundary=--AAA');
    Try
        WinHttpReq.Send(
        '----AAA'+#13#10+
        'Content-Disposition: form-data; name="message"'+#13#10+#13#10+
        #13#10+'['+FormatDateTime('yyyy-mm-dd hh:nn:zz', Now)+' : Test-MVD-API'+']'+#13#10+
        '----AAA'+#13#10+
        'Content-Disposition: form-data; name="imageFile"; filename="'+ ExtractFileName(FileName) +'"'#13#10 +
        'Content-Type: image/png'+#13#10+#13#10+
        #13#10+'----AAA--'+#13#10);
        Form1.Memo1.Lines.Add( WinHttpReq.Responsetext );
        Except
        Showmessage(ExceptionMessage) ;
    End;
end;
begin
    WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
end.
My Visual Database : I Love You
Easy For Beginner Student For Me

Re: How To use "WinHttp.WinHttpRequest.5.1" API Form Upload

Hi,


Why are you using ExtractFileName(FileName).
You are extracting the filename without the path.
In this case, the program tries to find it in its directory, but it is not there.
You need to specify the full name (path + name).

And with quotation marks in the send request, be more careful.

Re: How To use "WinHttp.WinHttpRequest.5.1" API Form Upload

sparrow wrote:

Hi,


Why are you using ExtractFileName(FileName).
You are extracting the filename without the path.
In this case, the program tries to find it in its directory, but it is not there.
You need to specify the full name (path + name).

And with quotation marks in the send request, be more careful.


I try Full path+name Result Error {"status":400,"message":"Invalid image."} How To Edit Script ?

         '----AAA'+#13#10+
         'Content-Disposition: form-data; name="message"'+#13#10+#13#10+
         #13#10+'['+FormatDateTime('yyyy-mm-dd hh:nn:zz', Now)+' : Test-MVD-API'+']'+#13#10+
         '----AAA'+#13#10+
         'Content-Disposition: form-data; name="imageFile"; filename="C:\Pic\pas.png"'#13#10 +
         'Content-Type: image/png'+#13#10+#13#10+
         #13#10+'----AAA--'+#13#10
My Visual Database : I Love You
Easy For Beginner Student For Me

6 (edited by sparrow 2023-08-16 20:26:44)

Re: How To use "WinHttp.WinHttpRequest.5.1" API Form Upload

Hi,


At the moment, I can offer several options for working with LINE without using 'WinHttp.WinHttpRequest.5.1'.
Sending messages and file:
1. using "CURL" (supplied with Windows 10 and 11)
2. using a small "SEND" utility (included in the sample kit).
"SEND" ONLY for working with LINE.
The utility has its own launch prompt if run without arguments.
The example is fully working.


There are some drawbacks, like returning a response in MVD . In my opinion, uncritical but still.
"CURL" can write the response to a file, "SEND" returns in the form of messages (errors and info)
to the screen.
We do not consider other possibilities due to their implementation (installation of additional services
for Windows and work through them directly with dll).
It also remains possible to use 'WinHttp.WinHttpRequest.5.1' for MVD, but only for messages.