Receiving File

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
Content-Disposition: form-data; name="Input"; filename="CN27110A.RET"
Content-Type: application/octet-stream"

And in the end...

Why it happenning? And how fix it?


What is the code for sending/receiving the file?

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;


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

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

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

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

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

When I received became this
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.

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.

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:

1 Like

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

My code:

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

cNomeArqTemp: String;
cLocalArq, cNomeArqFinal: String;


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


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"

1 Like

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