Ian Branch 128 Posted August 7, 2023 Hi Team, My Customer has asked me if I can add SMS sending to his Apps. In support he has sent me a document from Vodafone, he is a repairer for Vodafone, with the following information. I know absolutely nothing about REST and/or its usage in/with Delphi. Could I prevail on someone to create a small delphi unit that incorporates whatever is necessary to send an sms? It would be very much appreciated. Regards & TIA, Ian Share this post Link to post
Patrick PREMARTIN 82 Posted August 7, 2023 Hi Delphi has TNetHTTPClient/TNetHTTPRequest components and THTTPClient/THTPRequest classes to work with http(s) requests. You can use them to POST something if you want. Look at btnSendFileClick method in https://github.com/DeveloppeurPascal/Des-solutions-de-synchronisations-de-donnees/blob/master/04-Internet/Client/Unit1.pas or how I use POST request in the DeepL4Delphi library at https://github.com/DeveloppeurPascal/DeepL4Delphi And if you want to use TRESTClient/TRESTRequest, you can use the REST Debugger available in "tools" menu of the IDE to do a sample request and export the components from it. Share this post Link to post
Mohammed Nasman 11 Posted August 7, 2023 (edited) Hi Ian, It seems an easy one, PM me and I will help you. Regards, Edited August 7, 2023 by Mohammed Nasman Share this post Link to post
mjustin 23 Posted August 7, 2023 (edited) It is a very simple POST method request to a HTTPS address and with a JSON body. A small example how this can be done with Indy (included in Delphi) is here: https://mikejustin.wordpress.com/2015/03/14/indy-10-6-https-post/ Line 16 defines the body of the message. Replace it with your required JSON string. Line 21 send the message, change the server address to your destination server. It requires the OpenSSL DLLs which can be downloaded from https://github.com/IndySockets/OpenSSL-Binaries Edited August 7, 2023 by mjustin Share this post Link to post
Ian Branch 128 Posted August 9, 2023 Hi Team, I haven't forgotten this, just dealing with some issues for a Customer. Share this post Link to post
Sherlock 663 Posted August 10, 2023 On 8/7/2023 at 5:01 PM, Mohammed Nasman said: Hi Ian, It seems an easy one, PM me and I will help you. Regards, Yeah, I'm sorry, but that is not how this forum works. If every question would be resolved via PM, anyone looking for an answer to the same questions would get to find nothing here... and have no motivation to share his knowledge in this forum. 2 Share this post Link to post
Mohammed Nasman 11 Posted August 10, 2023 (edited) 1 hour ago, Sherlock said: Yeah, I'm sorry, but that is not how this forum works. If every question would be resolved via PM, anyone looking for an answer to the same questions would get to find nothing here... and have no motivation to share his knowledge in this forum. You are right, sorry for that. But because he said he doesn't know anything about rest api, so I was going to help to guide him to start doing that. and that will not answer the question directly Edited August 10, 2023 by Mohammed Nasman 1 Share this post Link to post
David Schwartz 430 Posted August 12, 2023 On 8/10/2023 at 3:44 AM, Mohammed Nasman said: You are right, sorry for that. But because he said he doesn't know anything about rest api, so I was going to help to guide him to start doing that. and that will not answer the question directly There are plenty of Delphi examples that could be cited as guides. Google this: "how to query a rest api using delphi" Even asking ChatGPT can elicit a decent response Share this post Link to post
Ian Branch 128 Posted August 12, 2023 Hi Team, I'm back. Thank you for your inputs. I have this so far: procedure TForm12.Button1Click(Sender: TObject); begin // // The RESTClient has already been configured. // RESTRequest1.AddParameter('username', 'email@email.com'); RESTRequest1.AddParameter('secret', 'password'); RESTRequest1.AddParameter('shortcode', 'pacificbulksms'); RESTRequest1.AddParameter('msisdn', '6799998122'); // I take it this is the destination mobile Phone #?? RESTRequest1.AddParameter('message', 'Hello World'); // Call the REST API and handle the response RESTRequest1.Execute; // Access the response content if RESTResponse1.StatusCode = 200 then begin // Process the response content (e.g., RESTResponse.Content) var sResponse := RESTResponse1.Content; Label1.Caption := sResponse; // // Example response {"result":"success","code":"200","data":{"text":"SMS sent successfully."}} var sResult := GetResponseParameters(sResponse, 3, 4); var sCode := GetResponseParameters(sResponse, 7, 8); var sMessage := GetResponseParameters(sResponse, 13, 14); end else begin // Handle error cases end; // end; The RESTClient has the BaseURL set as "https://pacificbulksms.com". The RESTRequest has the Resource set as "api/restJson". Are there any other default settings I need to change, or settings I need to make? Aside of course getting actual usable test parameters from Vodaphone. 😉 Regards & TIA, Ian Share this post Link to post
Ian Branch 128 Posted August 12, 2023 (edited) So, I got test parameters from Vodafone. When I run the App I get a 'Empty Request...' message. Clearly I have missed setting something. 😞 Ian Edited August 12, 2023 by Ian Branch Share this post Link to post
Patrick PREMARTIN 82 Posted August 13, 2023 10 hours ago, Ian Branch said: So, I got test parameters from Vodafone. When I run the App I get a 'Empty Request...' message. Clearly I have missed setting something. 😞 Ian The URL ? Did you tried with REST Debugger to be sure you have all parameters ? Share this post Link to post
Ian Branch 128 Posted August 13, 2023 Hi Patrick, When I put the Parameters into the REST Debugger, it says it is successful. When I have the parameters in the RESTRequest and right-click|Run, it is successful. But, If I leave The Client and Request exactly the same and inthe Delphi App do RESTRequest1.Execute, it fails. 😞 procedure TForm12.btnComponentsClick(Sender: TObject); var obj, code, data: TJSONObject; sCode, url: string; begin // // Call the REST API and handle the response RESTRequest1.Execute; // ShowMessage('Status Code = ' + RESTResponse1.StatusCode.ToString); obj := RESTResponse1.JSONValue as TJSONObject; scode := obj.Values['code'].Value; data := obj.Values['data'] as TJSONObject; url := data.Values['text'].Value; ShowMessage('Code = ' + scode + sLineBreak + 'Text = ' + url); end; Returning 'Status Code = 200' for the first ShowMessage, and 'Code = 302 Text = Empty Request.....' for the second showmessage. 😞 Regards, Ian Share this post Link to post
Uwe Raabe 2064 Posted August 13, 2023 May be I missed it, but I cannot see where you set RESTRequest1..Method to rmPost (the default is rmGet). The default parameter kind is pkGetOrPost, which put the parameters in the URL for GET and into the body for POST. This would explain the "Empty Request..." error. Share this post Link to post
Ian Branch 128 Posted August 13, 2023 Correction! I thought it had been successful in the Debugger but I was looking in the wrong place. 😞 Still an Empty Request error. This is the Debugger Request Tab: This is the Parameters Tab, with key info changed: 😉 You will note the 'Response' says 200:OK..... But the Tabular Data Tab says it was an error. I spoke to the Phone owner and he hasn't received any messages. I'm confused. Ian Share this post Link to post
Ian Branch 128 Posted August 13, 2023 Hi Uwe, It is set in the RESTRequest component. Ian Share this post Link to post
Ian Branch 128 Posted August 13, 2023 (edited) 15 minutes ago, Uwe Raabe said: The default parameter kind is pkGetOrPost, Hi Uwe, What should this parameter be? All the parameters basically look like this: Ian Edited August 13, 2023 by Ian Branch Share this post Link to post
Patrick PREMARTIN 82 Posted August 13, 2023 (edited) 7 minutes ago, Ian Branch said: Hi Uwe, What should this parameter be? Ian It depends on how the API expect to receive them. The GET/POST is use as GET param when you send a GET request and as POST params when you send a POST request. The status code 200 is normal : it's the http/s return status code. It says the URL you called has answered. As we see in the screen capture, Vodafone answered with a JSON object with 3 properties : result, code, and data as a JSON object. Fix the problem in REST debugger and export the components. Open a text editor (or Delphi's code editor), paste the result and you'll have all properties of the components to change in the form editor or by code depending on what you want to do. Edited August 13, 2023 by Patrick PREMARTIN Share this post Link to post
Patrick PREMARTIN 82 Posted August 13, 2023 As I understand it, your doc integration displays a REQUEST with a JSON as the body of the request. It's not a POST/GET standard call... and they must not having called it "REST API". Try to pass the parameters as BODY fields instead of GET/POST. Other way could be to copy/paste the object JSON generated with your parameters directly in the first tab in REST Debugger as "personalized body". Share this post Link to post
Ian Branch 128 Posted August 13, 2023 Hi Patrick, 7 minutes ago, Patrick PREMARTIN said: Try to pass the parameters as BODY fields instead of GET/POST. You mean put this: "{"username": "email@mail.com", "secret":"password","shortcode":pacificbulksms","msisdn":"6798688008","message":"Hello James..."}" into the Custom body? With the correct values of course. 😉 I just tried that, same result. Ian Share this post Link to post
Patrick PREMARTIN 82 Posted August 13, 2023 very strange does they have a public doc and sandbox ? Share this post Link to post
Ian Branch 128 Posted August 13, 2023 This was the doc they supplied me. SMS Gateway API Specifications v2.0.pdf Share this post Link to post
Uwe Raabe 2064 Posted August 13, 2023 IMHO the best way ist to use a dedicated class handling the parameters and place that in body: type TMyParam = class private FUserName: string; FSecret: string; FShortcode: string; FMsisdn: string; FMessage: string; public property Message: string read FMessage write FMessage; property Msisdn: string read FMsisdn write FMsisdn; property Secret: string read FSecret write FSecret; property Shortcode: string read FShortcode write FShortcode; property UserName: string read FUserName write FUserName; end; ... var myParam := TMyParam.Create; try myParam.UserName := 'email@email.com'; myParam.Secret :='password'; myParam.Shortcode := 'pacific bulkms'; myParam.Msisdn := '6799998122'; myParam.Message := 'Hello World'; RestRequest1.AddBody(myParam); finally myParam.Free; end; 2 Share this post Link to post
Ian Branch 128 Posted August 14, 2023 CRACKED IT!!!! As Patrick suggested, put the Json request into the Custom body. It didn't work for me the first time as I suspect I had the Json constructed wrong, and I still had Parameters in the RESTRequest. All sorted now. Now to actually create the App. 🙂 My thanks to all that contributed to my first foray into REST. Appreciated. Regards, Ian 1 Share this post Link to post
Patrick PREMARTIN 82 Posted August 14, 2023 Glad it's up and running. Don't hesitate to give a slap in my name to those people who make a REST API that isn't a REST API just because they use a POST. 1 Share this post Link to post
mvanrijnen 123 Posted August 14, 2023 4 hours ago, Patrick PREMARTIN said: Glad it's up and running. Don't hesitate to give a slap in my name to those people who make a REST API that isn't a REST API just because they use a POST. Why could a POST not be a port of a REST API interface? 1 Share this post Link to post