0

我正在尝试将附件添加到一项服务。这取自文档: 在此处输入图像描述

我想知道是否可以在 indy 中添加一个实体,就好像它可以在例如 Java 中完成一样:

postRequest.setHeader("X-Atlassian-Token","nocheck");
MultipartEntity entity = new MultipartEntity();
entity.addPart("file", new FileBody(fileUpload));
postRequest.setEntity(entity);
HttpResponse response = httpClient.execute(postRequest);
4

1 回答 1

1

找到了:

uses IdMultipartFormData
...
Stream: TIdMultipartFormDataStream;

已编辑: 对于 Jira REST API 的这个特殊问题,解决方案类似于:

针对 URL 发布:BASE_URL+/rest/api/2/issue/{issueIdOrKey}/attachments

try
    lHTTP.Request.CustomHeaders.AddValue('X-Atlassian-Token', 'nocheck');
    FileSize := lHTTP.Response.ContentLength;

    FileStrm := TFileStream.Create(AFile, fmOpenRead or fmShareDenyWrite);
    try
      if FileSize < FileStrm.Size then
      begin
        FileStrm.Position := FileSize;

        Stream := TIdMultipartFormDataStream.Create;
        try
          Stream.AddFile('file', AFile);

          with lHTTP do
          begin
            with Request do
            begin
              ContentRangeStart := FileSize + 1;
              ContentRangeEnd := FileStrm.Size;
            end;

            Post(self.BASE_URL + SEND_ATTACHEMENT_TO_AN_ISSUE_URL +
              IntToStr(IssueID) + '/attachments', Stream);

            Result := true;

          end;
        finally
          Stream.Free;
        end;
      end;
    finally
      FileStrm.Free;
    end;
  except
    Result := false;
  end;

注意:之后不要忘记改回标题并将“内容类型”更改为未来请求所需的类型

于 2013-05-31T10:41:46.490 回答