i would be very happy if someone could maybe help me with this. I am trying to send a file (.csv) using NetHTTPRequest (or anything else what would work) and PUT and api. It has to be PUT because the server doesn't accept anything else. I also need an api to log in.
In Powershell it would look like this:
The Client would be d329krpq (for example).
Invoke-WebRequest `
-UseBasicParsing `
-Uri https://example.com/d329krpq/filename.csv `
-Method PUT `
-InFile filename.csv `
-Headers @{'x-api-key'='lXViWTzFic9sM8Qqe9Ew7JME8xTdBAOMJHdIjK7XkjQ00OWr'}
Thank you very much! Arne
CodePudding user response:
Before starting, you have to correctly set the URL of your server resource : it must end with the endpoint/resource name, not the destination of your uploaded file. Like this example (the server is a PHP script) : https://example.com/uploader.php
To send a "PUT" request in the "TNetHTTPRequest" or "TNetHTTPClient" or "THTTPClient" component, you can use any of its "Put" overloaded methods. There is a version of the "Put" method where you can specify the server URL, the path to your file on your local drive or storage, and other optional parameters for the response stream and request headers. After processing the request, the function will return an instance of "IHTTPResponse '' that you can access its "ContentStream" property to check the response of your server. Afterward, it is up to your server to retrieve the uploaded file from the request content.
To proceed, just drop a "TNetHTTPRequest" control from the palette on your form, and create a procedure to "Put" your file, like this:
procedure TForm1.PutFile;
begin
NetHTTPRequest1.Put('https://example.com/uploader.php', 'C:\MyFile.csv');
end;
Or, if you want to create and destroy components on the fly without dropping anything on the form, you can proceed like this:
procedure TForm1.PutFile;
var
LHTTP: TNetHTTPClient;
begin
LHTTP := TNetHTTPClient.Create(nil);
try
LHTTP.Put('https://example.com/uploader.php', 'C:\MyFile.csv');
finally
LHTTP.Free;
end;
end;
If you want to send multiple files, then you can use the overloaded function with the "TMultipartFormData" object. You can create a "TMultipartFormData" object and call its "AddFile" function to specify the name (for distinguishing the file the request), the path and content type of your file. And then you "put" the object, like this example :
procedure TForm1.PutFile;
var
LHTTP: TNetHTTPClient;
LFormData: TMultipartFormData;
begin
LHTTP := TNetHTTPClient.Create(nil);
LFormData := TMultipartFormData.Create();
try
LFormData.AddFile('File1', 'C:\MyFile.csv');
LHTTP.Put('https://example.com/uploader.php', LFormData);
finally
LHTTP.Free;
LFormData.Free;
end;
end;
If your server does not handle Multipart form data, then you can check other solutions. We recommend using the REST Client library, where you can simply use the TRESTRequest "AddFile". You only have to specify the name (to distinguish the file in the request's content), its path and its content type.
Drop the components "TRESTClient", "TRESTRequest" and "TRESTResponse" on the form, and write your code. Like in this example:
procedure TForm1.PutFile;
begin
try
try
RESTRequest1.Method := TRESTRequestMethod.rmPUT;
RESTRequest1.AddFile('File1', 'C:\MyFile.csv', TRESTContentType.ctTEXT_CSV);
RESTRequest1.Execute;
except
ShowMessage('Uploading failed');
end;
finally
ShowMessage(RESTRequest1.Response.Content); // here you can see your server's response
end;
end;
CodePudding user response:
Thank you very very much for answering! I am completely stuck with this and have been trying since I posted my question. Could you maybe pls. help me with this. I need to upload a csv file to a server. They have certain Must Haves which are: Requirements (MUST)¶ Must be an HTTP PUT request. Must contain the Client ID in the URL request ({Client ID} would be replaced). Example: d329krpq. Must contain the stock file name that will be received ({Stock File Name} would be replaced), which can be different from your local file name. Example: 20181023_update.csv Must include the stock CSV file. Must contain the API Key in the headers (header name: x-api-key). Example: lXViWTzFic9sM8Qqe9Ew7JME8xTdBAOMJHdIjK7XkjQ00OWr.
How can I translate this for example into Delphi? curl -v -X PUT https://merchants-connector-importer.zalandoapis.com/d329krpq/20181023_update.csv --upload-file 20181023_update.csv --header "x-api-key: lXViWTzFic9sM8Qqe9Ew7JME8xTdBAOMJHdIjK7XkjQ00OWr"
Thank you so much! Arne