Message example below {" image_content ":" image base64 content ", "id" : "12345678",}
HTTP interface methods (post), request body is json
Excuse me, how to use Delphi implementation, give a code, thank you
CodePudding user response:
The function TWebAPI_JSON. Pic_Update (ABase64Img: String; Var APic: TPic; Var AErrStr: String; ARsultErrMsg: Boolean=False) : Boolean;The function CreateIdHttp: TCustomIndyHttp;
The begin
Result:=TCustomIndyHttp. Create (nil);
Result. AllowCookies:=true;
Result. HandleRedirects:=true;//allow the head to
Result. ReadTimeout:=5000;//request timeout
Result. ConnectTimeout:=15000;
Result. The Request. The ContentType:='application/json;//set the content type for jso
Result. The Request. Accept:='application/json;
end;
Const
//URL='/Picasso was - wan/governance - API/v1/file_upload. Json';
URL='http://... ';
JSONStr='{" fileStr ":" string "} ";
Var
AJSon AResult: ISuperObject;
SendJSONStream: TStringStream;
AResultStr: string;
AIdhttp: TCustomIndyHttp;
AItem: TSuperArray;
The begin
AIdhttp:=CreateIdHttp;
AIdhttp. ConnectTimeout:=15000;
AIdhttp. ReadTimeout:=15000;
AJSon:=SO (JSONStr);
Result:=False;
AJSon. [' id '] S:='12345678';
AJSon. [' image_content '] S:='images of base64 content;
SendJSONStream:=TStringStream. Create (UTF8Encode (AJSon. AsString) are identical);
Try
Try
AResultStr:=AIdhttp. Post (FHostURL + URL, SendJSONStream);
AResult:=SO (AResultStr);
If AResult. [b] 'success' then
The begin
If Assigned (AResult [' collection ']) then
The begin
AItem:=AResult [' collection '] AsArray;
If AItem. Length & gt; 0 then
The begin
APic. FileUrl:=AItem [0] [' fileUrl] AsString;
APic. FileUrl:=StringReplace (APic) fileUrl, 'the. JPG. JPG', 'JPG', [rfReplaceAll rfIgnoreCase]);
end;
Result:=True;
end;
AErrStr:='countless according to return;
end;
Except,
On e: EIdHTTPProtocolException do
The begin
AResultStr:=e.E rrorMessage;
AResult:=SO (AResultStr);
If Assigned (AResult [' message ']) then
The begin
AErrStr:=AResult [' message '] AsString;
End
The else
The begin
If not ARsultErrMsg then
AErrStr:=GetErrStr (e)
The else
AErrStr:=e.M essage.
end;
end;
On e: the Exception do
The begin
If not ARsultErrMsg then
AErrStr:=GetErrStr (e)
The else
AErrStr:=e.M essage.
end;
end;
The finally
AIdhttp. Free;
SendJSONStream. Free;
end;
end;
CodePudding user response:
procedure PostImage;
Var
JsonObj: TJsonObject;
IdHttp: TIdHTTP;
PostData: UTF8String;
Resp: string;
ReqStm: TStringStream;
The begin
ReqStm:=TStringStream. Create (");
Try
JsonObj:=TJsonObject. Create;
Try
JsonObj. AddPair (' image_content ', 'image base64 content);
JsonObj. AddPair (' id ', '12345678');
PostData:=UTF8Encode jsonObj. ToString ();
ReqStm. WriteBuffer (BytesOf (postData), Length (postData));
The finally
FreeAndNil (jsonObj);
end;
IdHttp:=TIdHTTP. Create;
Try
Try
IdHttp. AllowCookies:=True;
IdHttp. ConnectTimeout:=30000;
IdHttp. Request. ContentType:='application/json. Charset=utf-8 ';
IdHttp. Request. Accept:='*/*';
Resp:=IdHttp. Post (' your URL, reqStm);
If IdHttp. ResponseCode=200 then
The begin
//the processing of you
end;
Except ON E: the Exception do
end;
The finally
FreeAndNil (IdHttp);
end;
The finally
FreeAndNil (reqStm);
end;
end;