【问题标题】:Creating file using GitHub API with Delphi使用 GitHub API 和 Delphi 创建文件
【发布时间】:2022-07-21 22:18:40
【问题描述】:

我正在尝试将 GitHub API 与 Delphi REST 组件一起使用在存储库中创建文件。我已经通过 Python 和 curl 调用成功完成了这项工作,但经过大量努力,我似乎​​无法使用提供的 REST 组件从 Delphi 让它工作。我已经使用 Delphi 组件成功完成了 GET。有效的 curl 命令是:

curl -X PUT \
  -H "Authorization: token ghp_xxxxxxxxxxxxxxxxxxxxxxxx"   
   https://api.github.com/repos/<user>/TestRepo/contents/test.txt \
   -d '{"message": "Add File", "content": "bXkgbmV3IGZpbGUgY29udGVudHM="}'

我已经换掉了用户名并隐藏了令牌,但这个调用有效。

我使用的等效 Delphi 代码是:

procedure TfrmMain.addFile;
begin
  RESTClient1.BaseURL := 'https://api.github.com';
  RESTRequest1.Client := RESTClient1;
  RESTRequest1.Resource := '/repos/<user>/TestRepo/contents/test.txt';
  RESTRequest1.Method := rmPUT;
  RESTRequest1.AddParameter('Authorization', 'ghp_xxxxxxxxxxxxxxxxx', pkHTTPHEADER);
  RESTRequest1.AddParameter('message', 'Add File', pkREQUESTBODY);
  RESTRequest1.AddParameter('content', 'bXkgbmV3IGZpbGUgY29udGVudHM=', pkREQUESTBODY);
  RESTRequest1.Execute;
  Memo1.text := RESTResponse1.JSONValue.ToString;
end;

我得到的回应是:

{"message":"Not 
Found","documentation_url":"https:\/\/docs.github.com\/rest\/reference\/repos#create-or- 
update-file-contents"}

我也尝试过使用 Delphi REST 调试器,我得到了同样的错误信息。

我尝试改变

  RESTRequest1.AddParameter('Authorization', 'ghp_xxxxxxxxxxxxxxxxx', pkHTTPHEADER);

  RESTRequest1.AddParameter('Authorization', 'token ghp_xxxxxxxxxxxxxxxxx', pkHTTPHEADER);

以防万一这是问题,但没有区别。有什么建议吗?

【问题讨论】:

    标签: rest delphi


    【解决方案1】:

    对正文使用 AddParameter 是错误的。生成的内容不会自动为 JSON。试试这个方法:

      RESTClient1.BaseURL := 'https://api.github.com';
      RESTRequest1.Client := RESTClient1;
      RESTRequest1.Resource := 'repos/<user>/TestRepo/contents/test.txt';
      RESTRequest1.Method := rmPUT;
      RESTRequest1.AddAuthParameter('Authorization', 'token ghp_xxxxxxxxxxxxxxxxx', pkHTTPHEADER);
      RESTRequest1.AddBody(TJSONObject.Create.AddPair('message', 'Add File').AddPair('message', 'Add File'), ooREST);
      RESTRequest1.Execute;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-02
      • 2023-03-10
      • 1970-01-01
      • 2022-10-06
      • 2023-03-15
      • 2013-09-11
      • 2019-05-08
      • 1970-01-01
      相关资源
      最近更新 更多