본문 바로가기
Delphi Tip/인터넷

idHTTP JSON Post 방법

by MonoSoft 2021. 12. 13.
728x90
반응형

idHTTP JSON Post 방법 

 

토큰 키값이 존재 시 다음과 같이 처리한다.

 

procedure TForm1.Button6Click(Sender: TObject);

var

URL, Retorno: String;

JsonStreamRetorno, JsonStreamEnvio: TStringStream;

begin

URL := 'https://0000000000000t';

//UTF-8로 받는다

JsonStreamEnvio := TStringStream.Create(Memo2.Text,TEncoding.UTF8);

JsonStreamRetorno := TStringStream.Create('');

// Init request:

try

idHttp.Request.Clear;

idHttp.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);

idHttp.HandleRedirects := True;

idHttp.Request.Clear;

idHttp.Request.BasicAuthentication := true;

idHttp.Request.Accept := '*/*';

idHttp.Request.CustomHeaders.Add('000');

idHttp.Request.CustomHeaders.Add('000');

idHttp.Request.ContentType := 'application/json; charset=utf-8';

idHttp.Request.ContentEncoding := 'utf-8';

 

try

idHttp.Post(URL, JsonStreamEnvio, JsonStreamRetorno);

Memo2.Text := IdHTTP.Post(URL, JsonStreamEnvio);

except

on E:EIdHTTPProtocolException do

Memo2.Lines.Add(e.ErrorMessage);

end;

finally

memo2.Lines.add(JsonStreamRetorno.DataString);

end;

end;

 

=========================================

procedure Tformmaintestbed.btnJSONSendClick(Sender: TObject);

var

code: Integer;

sResponse: string;

JsonToSend: TStringStream;

begin

JsonToSend := TStringStream.Create(

'{"auth": {"applicationId": "' + edApplication.text +

'","applicationPassword": "' + edpassword.text +

'","accountId": "' + edaccount.text +

'","userId": "' + edUser.text +

'"}}');

try

HTTP1.Request.ContentType := 'application/json';

HTTP1.Request.ContentEncoding := 'utf-8';

memoRequest.lines.clear;

memoRequest.lines.add(JsonToSend);

try

sResponse := HTTP1.Post(cbAddress.text, JsonToSend);

except

on E: Exception do

ShowMessage('Error on request: '#13#10 + e.Message);

end;

 

memoResponse.lines.clear;

memoresponse.lines.add(sResponse);

finally

JsonToSend.Free();

end;

end;

 

728x90
반응형

댓글