用HttpClient封装Curl发送graphQL到shopifyAPI
在springboot环境下编写先放示例代码:String url = "https://" + request.getShopName() + "/admin/api/" + request.getApiVersion() + "/graphql.json";CloseableHttpClient httpClient = HttpClients.createDefault();HttpPost
·
在springboot环境下编写
先放示例代码:
String url = "https://" + request.getShopName() + "/admin/api/" + request.getApiVersion() + "/graphql.json";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
// 设置头部信息
httpPost.addHeader("X-Shopify-Access-Token", request.getAccessToken());
httpPost.addHeader("Content-Type", "application/json");
// 创建查询体
JSONObject query = new JSONObject();
query.put("query",
StringQuery
);
String responseContent = null;
try {
// 将查询体设置到实体中
StringEntity input = new StringEntity(query.toString());
httpPost.setEntity(input);
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
responseContent = EntityUtils.toString(entity,"UTF-8");
response.close();
httpClient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return responseContent;
我看网上关于调用shopify的文章不多,然后踩了不少坑,写一篇记录一下。
1,url的/admin 没有添加导致一直请求不到shopify(看官方文档时看错了,而且用AI自动生成时不一定会有admin
String url = "https://" + request.getShopName() + "/admin/api/" + request.getApiVersion() + "/graphql.json";
2,由于一开始用postman测的时候,直接就写语句,没注意,后面封装的时候,少了query:,查原因的时候,人都麻了。
3,如果觉得自己的代码写的没问题,但是报以下错误,很有可能是VPN代理问题,关掉后,重启一下电脑说不定问题就解决了。(本人就是,测试环境可以运行,但在本地环境一直报这个错误,人都麻了)
更多推荐
所有评论(0)