我正在尝试使用Delphi组件的GitHub API在回购中创建一个文件。我已经通过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调试器,并得到了相同的错误消息。
我试着改变
RESTRequest1.AddParameter('Authorization', 'ghp_xxxxxxxxxxxxxxxxx', pkHTTPHEADER);至
RESTRequest1.AddParameter('Authorization', 'token ghp_xxxxxxxxxxxxxxxxx', pkHTTPHEADER);以防万一,这是问题,但没有区别。有什么建议吗?
发布于 2022-07-21 14:18:40
将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, [poDoNotEncode]);
RESTRequest1.AddBody(TJSONObject.Create.AddPair('message', 'Add File').AddPair('content', 'bXkgbmV3IGZpbGUgY29udGVudHM='), ooREST);
RESTRequest1.Execute;https://stackoverflow.com/questions/73067566
复制相似问题