Delphi แนบรูปภาพใน Line Notify ด้วย NetHTTPClient และ NetHTTPRequest

Delphi เนี่ยมี Componet ให้ใช้สำหรับติดต่อฝั่ง API ได้หลายตัว วันนี้ขอนำเสนอ NetHTTPClient และ NetHTTPRequest ซึ่งแนะนำเป็น Delphi Version XE 10.3.3 Rio Update 3 นะครับ

เริ่มต้นโดยการออกแบบหน้าตา ตามความชอบ

จากนั้นก็ใส่เครื่องซอสสูตรลับ (Source Code) ลงไป

โดยกำหนดเป็น Function เอาไว้

Uses
System.Net.URLClient,System.Net.HttpClient,
System.Net.HttpClientComponent, System.Net.Mime
procedure LineSendCustomNotify(msg: RawByteString;token: String;
ImagePath :String)
;
procedure TForm9.LineSendCustomNotify(msg: RawByteString; token: String;ImagePath :  String);
Var
NetHTTPFormData : TMultipartFormData;

begin

token := TxtToken.Text;


NetHTTPClient1.ContentType := 'application/x-www-form-urlencoded';

NetHTTPFormData := TMultipartFormData.Create();
NetHTTPFormData.AddField('message',msg);
NetHTTPFormData.AddFile('imageFile',ImagePath);

NetHTTPClient1 := TNetHTTPClient.Create(nil);
NetHTTPRequest1 := TNetHTTPRequest.Create(nil);
NetHTTPRequest1.Client := NetHTTPClient1;
NetHTTPRequest1.MethodString := 'POST';
NetHTTPRequest1.CustomHeaders['Authorization'] := 'Bearer '+token;
NetHTTPRequest1.Post('https://notify-api.line.me/api/notify', NetHTTPFormData);




end;

เมื่อกดปุ่ม Send ก็ให้เรียกใช้

LineSendCustomNotify(TxtMessage.Text,TxtToken.Text,ImagePath.Text);

วิธีนี้ไม่ต้องใช้ SSL Library ให้ยุ่งยากครับ Run แล้วไปเลย

สำหรับใครที่อยากดูตัวอย่าง Source Code ก็สามารถ Download ได้ที่

https://github.com/piyanatn/linenotify_nethttpclient

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response

More from Piyanat Nimkhuntod