Recent

Author Topic: [SOLVED] Verbs HTTP  (Read 1457 times)

Almir Lima

  • New Member
  • *
  • Posts: 36
    • iSoft Tecnlogia Digital
[SOLVED] Verbs HTTP
« on: May 25, 2019, 06:25:31 pm »
Hy everyone,
I want send others verbs in HTTP like PUT PATCH DELETE with Synapse, but i cannot find.
« Last Edit: May 26, 2019, 04:00:51 am by Almir Lima »
Today better than yesterday.

Almir Lima

  • New Member
  • *
  • Posts: 36
    • iSoft Tecnlogia Digital
Re: Verbs HTTP
« Reply #1 on: May 26, 2019, 03:59:57 am »
The goal is do a API and use all RESTFUL verbs.
GET, POST, PUT, PATCH, DELETE, OPTIONS.

I solved this creating a clone function of the Synapse HttpSend with a little modify: "A option for alter the Verbs to send a request."

I tested  in NodeJS, and everything worked perfect.

This the code.

Code: Pascal  [Select][+][-]
  1. function Http_Send(const Verb, URL: string; const Data: TStream
  2.   ): Boolean;
  3. var
  4.   HTTP: THTTPSend;
  5. begin
  6.   HTTP := THTTPSend.Create;
  7.   try
  8.     HTTP.Document.CopyFrom(Data, 0);
  9.     HTTP.MimeType := 'Application/octet-stream';
  10.     Result := HTTP.HTTPMethod( Verb, URL);
  11.     Data.Size := 0;
  12.     if Result then
  13.     begin
  14.       Data.Seek(0, soFromBeginning);
  15.       Data.CopyFrom(HTTP.Document, 0);
  16.     end;
  17.   finally
  18.     HTTP.Free;
  19.   end;
  20. end;
  21.  
Today better than yesterday.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [SOLVED] Verbs HTTP
« Reply #2 on: May 26, 2019, 09:27:19 am »
A friendly reminder: When talking about networking you should have a care how you use the terminology, lest confussion arises.

Those "verbs" you talk about have little to do with RESTful APIs, but with WEBDAV. See here: WebDAV Ressources.

Also, if you want to build a generic solution you should add a functions to send multipart- in addition to URL-encoded  requests. Most Web APIs I know of expect them, since they deal with relatively big objects (documents, images, etc.) that can't be encoded in the URL.

For example, here's how we defined some net-helpers when we built our (Delphi) Flickr API objects:
Code: Pascal  [Select][+][-]
  1. { Calls to Web Sites' APIs like p.e. Flickr's }
  2. {HTTP service request--url-encoded params
  3.  Make a request using HTTP Method, passing parameters
  4.  @code(Params) in url-encoded form. }
  5. function WebMethodCall(const BaseURL: String; Params: TStrings;
  6.                        Method: TReqMethod = rqPost): String; overload;
  7. {HTTP service request--w/out params
  8.  Make a request using HTTP Method passing no parameters
  9.  (unless already encoded by the user in @code(BaseURL).}
  10. function WebMethodCall(const BaseURL: String;
  11.                        Method: TReqMethod = rqPost): String; overload;
  12. {HTTP service request--Multipart/Form-Data
  13.  Make a request using HTTP POST and passing parameters
  14.  as a Multipart/Form-Data stream.
  15.  Used, generally, to upload a file to a server.
  16.  }
  17. function WebMethodCall(const BaseURL: String;
  18.                        FormData: TIdMultiPartFormDataStream): String; overload;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018