delphi 中用http的post base64的图片和字符串两个参数问题,请帮忙解答下

its2000 2018-07-21 10:43:19
新手,高分求问:
报文示例如下{"image_content":"图片的base64内容","id":"12345678",}
接口方法http(post)方式,请求body是json
请问怎么用delphi实现,给个代码吧,谢谢
...全文
574 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
浮生-若梦 2018-07-24
  • 打赏
  • 举报
回复
procedure PostImage;
var
jsonObj: TJsonObject;
IdHttp: TIdHTTP;
postData: UTF8String;
resp: string;
reqStm: TStringStream;
begin
reqStm := TStringStream.Create('');
try
jsonObj := TJsonObject.Create;
try
jsonObj.AddPair('image_content','图片的base64内容');
jsonObj.AddPair('id','12345678');
postData := UTF8Encode(jsonObj.ToString);
reqStm.WriteBuffer(BytesOf(postData), Length(postData));
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('你的URL', reqStm);
if IdHttp.ResponseCode = 200 then
begin
//你的处理
end;
except ON E:Exception do

end;
finally
FreeAndNil(IdHttp);
end;
finally
FreeAndNil(reqStm);
end;
end;
大肚肥肥 2018-07-23
  • 打赏
  • 举报
回复
function TWebAPI_JSON.Pic_Update(ABase64Img: String; var APic: TPic; var AErrStr: String; ARsultErrMsg: Boolean = False): Boolean;

function CreateIdHttp: TCustomIndyHttp;
begin
Result := TCustomIndyHttp.Create(nil);
Result.AllowCookies := true;
Result.HandleRedirects := true; // 允许头转向
Result.ReadTimeout := 5000; // 请求超时设置
Result.ConnectTimeout := 15000;

Result.Request.ContentType := 'application/json'; // 设置内容类型为jso
Result.Request.Accept := 'application/json';
end;

const
// URL = '/picasso-wan/governance-api/v1/file_upload.json';
URL = 'http://..........';
JSONStr = '{ "fileStr": "string"}';
var
AJSon, AResult: ISuperObject;
SendJSONStream: TStringStream;
AResultStr: string;
AIdhttp: TCustomIndyHttp;
AItem: TSuperArray;
begin
AIdhttp := CreateIdHttp;
AIdhttp.ConnectTimeout := 15000;
AIdhttp.ReadTimeout := 15000;
AJSon := SO(JSONStr);
Result := False;
AJSon.S['id'] :='12345678';
AJSon.S['image_content'] := '图片的base64内容';
SendJSONStream := TStringStream.Create(UTF8Encode(AJSon.AsString));
try
try
AResultStr := AIdhttp.Post(FHostURL + URL, SendJSONStream);

AResult := SO(AResultStr);
if AResult.b['success'] then
begin
if Assigned(AResult['collection']) then
begin
AItem := AResult['collection'].AsArray;
if AItem.Length > 0 then
begin
APic.fileUrl := AItem[0]['fileUrl'].AsString;
APic.fileUrl := StringReplace(APic.fileUrl, '.jpg.jpg', '.jpg', [rfReplaceAll, rfIgnoreCase]);

end;
Result := True;
end;
AErrStr := '无数据返回';
end;
except
on e: EIdHTTPProtocolException do
begin
AResultStr := e.ErrorMessage;
AResult := SO(AResultStr);
if Assigned(AResult['message']) then
begin
AErrStr := AResult['message'].AsString;
end
else
begin
if not ARsultErrMsg then
AErrStr := GetErrStr(e)
else
AErrStr := e.Message;
end;
end;
on e: Exception do
begin
if not ARsultErrMsg then
AErrStr := GetErrStr(e)
else
AErrStr := e.Message;
end;
end;
finally
AIdhttp.Free;
SendJSONStream.Free;
end;
end;

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧