新手,高分求问:
报文示例如下{"image_content":"图片的base64内容","id":"12345678",}
接口方法http(post)方式,请求body是json
请问怎么用delphi实现,给个代码吧,谢谢

解决方案 »

  1.   

    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;
      

  2.   

    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;