Receiving File

Hi
I have a service in order to process a file.
I received a file.
When I use PostMan, work fine.
But, when I use swagger, the file came with some caracters strangers to me.
In the begin of file
"------WebKitFormBoundaryJjjj4q1PAAqcY5Ou
Content-Disposition: form-data; name="Input"; filename="CN27110A.RET"
Content-Type: application/octet-stream"

And in the end...
'------WebKitFormBoundaryJjjj4q1PAAqcY5Ou--'

Why it happenning? And how fix it?

Thanks.

What is the code for sending/receiving the file?

Hi.
I send the file trought swagguer;
The code below is the service whitch receive the data and when I call the function "LerRetorno", a exception is raised because there is "dirt" in file.

"
function TBoletoService.ProcessarArqRetorno(ArquivoRetorno: TStream): TResultArqRetornoDTO;
var AcBrBoleto : TAcBrBoleto;
ImpressaoBoleto : TACBrBoletoFCFortes;
LStream: TFileStream;
nCount: Integer;
DadosRetornoArq: TItemResultArqRetornoDTO;

cNomeArqTemp: String;
cLocalArq, cNomeArqFinal: String;

begin

cNomeArqTemp := ApplicationPath+'ArqRetorno_'+FormatDateTime('ddmmyyhhnnsszz', Now)+'.ret';
Result := TResultArqRetornoDTO.Create;

LStream := TFileStream.Create(cNomeArqTemp, fmCreate);
LStream.CopyFrom(ArquivoRetorno, ArquivoRetorno.Size);
LStream.Free;

AcBrBoleto := TAcBrBoleto.Create(nil);
ImpressaoBoleto := TACBrBoletoFCFortes.Create(Nil);
AcBrBoleto.ACBrBoletoFC := ImpressaoBoleto;
AjustesObjImpressao(ImpressaoBoleto);

AcBrBoleto.NomeArqRetorno := cNomeArqTemp;
AcBrBoleto.LeCedenteRetorno := True;
AcBrBoleto.LerRetorno;

"
The begin of file, on the first line is
"02RETORNO01COBRANCA 073100785150 FA"

When I received became this
"------WebKitFormBoundarywqxnhABaPIaVkn9Q
Content-Disposition: form-data; name="Input"; filename="CN27110A.RET"
Content-Type: application/octet-stream"

Why the file are changing their content?

If you want more details, I can send to your email. There is some non public informations, so if you want, I can send in your e-mail thel all details.

Thanks.
Kind Regards

That's a limitation of Swagger-UI, that's how it sends files to the server. It sends files in multipart/form-data format. That's why it adds that extra information to the file content.

So either your XData service handle that format, or you can't use Swagger-UI.

Hi.
Understood
How can I handle this? There is a way? How I clear this? Or the problem is in the origin of requisition? If yes, which type of requisition we must work?

Thank you for your attention

Hi Fernando, I'm not sure what you mean?
It's just the way SwaggerUI works. That's how it sends the content of the files.

I want know where is the problem. By according you, the problem is in SwaggerUI. I can´t fixed this in my side on Xdata. Right?

It's not a "problem", but a limitation. SwaggerUI sends files in multipart/form-data format. If you want to handle that at XData side, you need to handle such content type.

You must check if content-type header is multipart/form-data, and if it is, you should handle the TStream considering the extra information sent by Swagger-UI.

You can use Sparkle TMultiPartFormDataReader for that. Check this topic: https://doc.tmssoftware.com/biz/sparkle/guide/server.html#handling-multipart-content

1 Like

Sorry !!!
But I don't understand how I to make this yet.

My code:

function TechoService.ProcessarArqRetorno(
ArquivoRetorno: TStream): String;
var
Reader: TMultipartFormDataReader;
LStream: TFileStream;
nCount: Integer;

cNomeArqTemp: String;
cLocalArq, cNomeArqFinal: String;

begin

Reader := TMultipartFormDataReader.Create(ArquivoRetorno, ????);

Thanks.

You can use this:

Reader := TMultipartFormDataReader.Create(ArquivoRetorno, TXDataOperationContext.Current.Request.Headers.Get('content-type');

There is also a Sparkle demo showing how to use TMultipartFormDataReader. It uses Sparkle directly, not XData, but the code will mostly the the same.

Works fine.
I had been not find the "Headers.Get"
Thanks.

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.