因此,每当我尝试执行带有正文的API调用时,我就会收到臭名昭着的“解析JSON问题”错误 . 我已经尝试了json的字符串化和非字符串化版本:
{"path":"bin/foo","message":"[SPS-CodeDeploy] Added routing file \u0027bin/foo\u0027","content":"","branch":"foobar"}
和:
"{\"path\":\"bin/foo\",\"message\":\"[Blah] Added file \\u0027bin/foo\\u0027\",\"content\":\"\",\"branch\":\"foobar\"}"
我对THIS api执行此操作,我能够通过curl运行这个确切的调用没有问题....我尝试设置内容类型,我试图在正文中指定'json',但无济于事 .
PS,我在Java中用apache OLTU OAuth库做这个 .
这是我的代码:
public String writeFile(String repoPath, String contents, String branchName) throws OAuthSystemException, OAuthProblemException {
String urlPath = String.format("/repos/%s/%s/contents/%s", repoOwner, repoName, repoPath);
String message = String.format("[Blah] Added file '%s'", repoPath);
GitHubCreateFileRequest gitHubCreateFileRequest = new GitHubCreateFileRequest(repoPath, message, contents, branchName);
OAuthClientRequest bearerClientRequest = buildRequest(urlPath);
OAuthResourceResponse resp = performRequest(bearerClientRequest, "PUT", new Gson().toJson(gitHubCreateFileRequest));
if (resp.getResponseCode() >= 200 && resp.getResponseCode() < 299) {
return branchName;
}
return null;
}
private OAuthClientRequest buildRequest(String urlPath) throws OAuthSystemException {
return new OAuthBearerClientRequest(String.format("%s%s", githubUrlPrefix, urlPath))
.setAccessToken(GITHUB_OAUTH_TOKEN).buildHeaderMessage();
}
private OAuthResourceResponse performRequest(OAuthClientRequest bearerClientRequest, String verb, String body) throws OAuthProblemException, OAuthSystemException {
if (body != null) {
bearerClientRequest.setBody(body);
}
bearerClientRequest.addHeader("Content-Type", "application/json");
return oAuthClient.resource(bearerClientRequest, verb, OAuthResourceResponse.class);
}
有任何想法吗?
PPS
我将授权 Headers 设置为承载abcd1234 ....而不是查询参数或正文