Recent

Author Topic: Microsoft Translation + Synapse  (Read 862 times)

bobkos

  • New member
  • *
  • Posts: 23
Re: Microsoft Translation + Synapse
« Reply #15 on: April 14, 2019, 12:33:17 am »
Unfortunaterly No.

In case if I put
Code: Pascal  [Select]
  1. FWebSession.MimeType:= 'application/json';
then the following reply
Code: [Select]
{"error":{"code":415000,"message":"The Content-Type header is missing or invalid."}}I suppose that when we have got answer code 415, apart of 400 then previously header was ok and problem is somewhere else.

Also not help
Code: Pascal  [Select]
  1. Headers.Add('Accept: */*');
and changing UserAgent. All this options I have tried previously. %)
« Last Edit: April 14, 2019, 12:35:00 am by bobkos »

Thausand

  • Full Member
  • ***
  • Posts: 172
Re: Microsoft Translation + Synapse
« Reply #16 on: April 14, 2019, 01:43:26 am »
I Sorry bobkos.

I forget because i not see first.
Add
Code: Pascal  [Select]
  1.   FWebSession.Protocol:='1.1';

Other thing i change was make
Code: Pascal  [Select]
  1.   FWebsession.KeepAlive:= false;
But maybe not want.

I read curl not set "connection" for header and i try remove from synapse. I only can make KeepAlive true or false and synapse header any time show "connection" in header. It no matter if connection header there or not.

No need change UserAgent.

Quote
{"error":{"code":415000,"message":"The Content-Type header is missing or invalid."}}
I think that is error you code because i not get this error.

Have you look precise you code and set mimetype after FWebSession.Clear line ?

Add:

This is work me:
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   FWebSession:THTTPSend;
  4.   ADocString:string;
  5. begin
  6.   FWebSession:=THTTPSend.Create;
  7.  
  8.   FWebSession.Clear;
  9.   FWebSession.Protocol:='1.1';
  10.   FWebSession.MimeType:='application/json';
  11.  
  12.   FWebSession.Headers.Add('Ocp-Apim-Subscription-Key: <HERE IS MY VALID KEY>');
  13.  
  14.   ADocString:='[{"Text":"car"}]';
  15.   FWebSession.Document.WriteBuffer(Pointer(ADocString)^,Length(ADocString));
  16.   FWebSession.Document.Position:=0;
  17.  
  18.   FWebSession.Headers.Add(Format('Content-Length: %d',[FWebSession.Document.Size]));
  19.  
  20.   if FWebSession.HTTPMethod('POST','https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=fr') then
  21.   begin
  22.     FWebSession.Document.SaveToFile('answer.json');
  23.   end;
  24.  
  25.   FreeAndNil(FWebSession);
  26. end;
« Last Edit: April 14, 2019, 03:12:27 am by Thausand »

bobkos

  • New member
  • *
  • Posts: 23
Re: Microsoft Translation + Synapse
« Reply #17 on: April 14, 2019, 01:54:23 pm »
You did it! Thank you Thausand.

Now I get what was wrong... When I declared type according to your advise, instead of
Code: Pascal  [Select]
  1. MimeType:= 'application/json'
it was
Code: Pascal  [Select]
  1. MimeType:= 'Content-Type: application/json'

That was my stuipid error!

I really appreciate your support.

Thausand

  • Full Member
  • ***
  • Posts: 172
Re: Microsoft Translation + Synapse
« Reply #18 on: April 15, 2019, 08:57:47 am »
That was my stuipid error!
He he. Is no problem. I make many error to and maybe i not write good for you understand what i mean.

Quote
I really appreciate your support.
That ok.

You forum write error and make help me better learn english and pascal/lazarus when try fix error you write.

I thank you for help me :)