When was p_web.RequestFiles.Item(n) added to NetTalk or is ChatGPT fibbing?

ChatGPT keeps telling me various methods (which none have compiled under NetTalk 11.52) that another API service can upload files to my NetTalk API service using a POST with “Content-Type” set to “multipart/form-data”. For a NetTalk API procedure of type “NetWebServiceMethod” what version is needed to support the p_web.RequestFiles.Item(n) methods that ChatGPT is telling me about or is it fibbing / hallucinating?

Hard to comment on your ChatGPT post, because you didnt include it, but im not aware (from memory) of anything called RequestFiles much less RequestFiles.Open

I’ve found ChatGPT to be somewhat useless though with regards to NetTalk advice.

So perhaps just ask us what it is you are actually trying to do?

I want to add a new method to a NetTalk API server (Ver 11.52, if possible) that can receive one or more files from another, non-NetTalk api, server where the files being posted are sent using “multi-part/form” content. The NT “NetWebServiceMethod” is expected to receive the temporary file names NT used to store the files. The original file names are also included. There will be other form data received that tells the NT method what to do with the files. The other api server would have some c# code like the following, but maybe modified to support sending more than one file at a time, depending on file sizes.

using System.Net.Http;
using System.Net.Http.Headers;
async Task UploadZipAsync(string apiUrl, string zipPath)
{
    using var client = new HttpClient();
    client.Timeout = TimeSpan.FromMinutes(30);
    using var form = new MultipartFormDataContent();
    // File content
    var fileStream = File.OpenRead(zipPath);
    var fileContent = new StreamContent(fileStream);
    fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
    form.Add(fileContent, "file", Path.GetFileName(zipPath));
    // Optional metadata
    form.Add(new StringContent("12345"), "jobId");
    // Auth + idempotency
    client.DefaultRequestHeaders.Add("X-Server-Token", "MySharedSecret");
    client.DefaultRequestHeaders.Add("Idempotency-Key", Guid.NewGuid().ToString());
    var response = await client.PostAsync(apiUrl, form);
    string body = await response.Content.ReadAsStringAsync();
    Console.WriteLine($"Status: {(int)response.StatusCode}");
    Console.WriteLine(body);
}

Here is a shortened version of one version of ChatGPT’s suggestion for the NetWebServiceMethod:

UploadFile &NetWebUploadedFile
TempName   STRING(260)
FinalName  STRING(260)
JobId      STRING(50)  ! Details omitted below on populating this field.
IdemKey    STRING(64)  ! Details omitted below on populating this field.
  code
! --- Validate upload ---
IF Request.Files.Count() = 0
   RETURN Self.JsonError(400, 'No file uploaded')
END
UploadFile &= Request.Files.GetFile('file')
IF UploadFile &= NULL
   RETURN Self.JsonError(400, 'Missing multipart field: file')
END
! --- Capture metadata ---
TempName  = UploadFile.TempFileName
FinalName = 'D:\InboundZips\' & UploadFile.FileName
! --- Move ZIP immediately ---
IF ~MOVE(TempName, FinalName)
   RETURN Self.JsonError(500, 'Failed to store uploaded file')
END
QueueZipForProcessing(FinalName, JobId)
RETURN Self.JsonResponse(200, |
   '{ "status": "accepted", "jobId": "' & JobId & '" }')

Here is another version of a NT service method that ChatGPT came up with that would accept multiple files uploaded with multi-part/form data:

i           LONG
UploadFile  &NetWebUploadedFile
TempName    STRING(260)
FinalName   STRING(260)
IF p_web.RequestFiles.Records() = 0
   RETURN Self.JsonError(400, 'No files uploaded')
END

LOOP i = 1 TO p_web.RequestFiles.Records()
   UploadFile &= p_web.RequestFiles.Item(i)

   IF UploadFile &= NULL
      CYCLE
   END

   TempName  = UploadFile.TempFileName
   FinalName = 'D:\InboundZips\' & UploadFile.FileName

   IF ~MOVE(TempName, FinalName)
      RETURN Self.JsonError(500, 'Failed to store uploaded file: ' & UploadFile.FileName)
   END

   QueueZipForProcessing(FinalName)
END

Lets talk about 1 file first, then discuss iploading multiple files at the same time. Also, ehile NT11 is quite far back, i dont think this has changed, so it shoukd work for you.

On the server side, one of the paramerter types is FILE. You use a StringTheory object for that field type (and you manually save it to disk if you wish to do so using stringTheory SaveFile.

On the server side, incoming requests always support form-encoded (multi part or not). In this case your client will switch to multi-part when uploading a file anyway (thats an HTTP thing).

Play around with the parameter types, and the generated Help and you’ll see various client examples.

Now onto multiple files;

First thing to decide is if this is better than just calling the YourOneFileUpload API multiple times. I’d argue that calling the API multiple times will be simpler code on both the client side and the server side. It also opens the door to faster performance because the server side is multi-threaded, and the clientside can have multiple sending objects.

Also given the overall POST size limitation you set on the server, one at a time allows for you to document it as “largest file that can be sent” rather than “largest combined set that can be sent”.

Now, assuming you really want multiple files, things get more complicated because each incoming file “parameter” needs a unique parameter name (distinct from the filename). Think File1, File2 and so on. Now you have a arbitrary maximum, so you might end up calling the API multiple times anyway.

I’d recommended keeping it simple, make an API to receive 1, and let the client call it multiple times.

(And yes, the ChatGPT answers are complete garbage.)

Rule #1 of AI … make the customer happy!

Translation: If you don’t know the answer … MAKE ONE UP!

Rule #2 of AI … It’s OK to lie about Rule #1.

:slight_smile: