[英]Java Servlet how to get the json response from Shopify API
我試圖通過REST服務從shopify API獲得產品記錄。 但是我遇到了這個錯誤{"errors":{"product":"can't be blank"}}
下面是代碼片段。
String getURL = "https://myshop.myshopify.com/admin/products.json";
url = new URL(getURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type",
"application-json");
connection.setRequestProperty("X-Shopify-Access-Token",token);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
DataOutputStream w = new DataOutputStream(
connection.getOutputStream());
w.flush();
w.close();
// Get Response
InputStream i = connection.getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(i, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null){
System.out.println(inputStr);
responseStrBuilder.append(inputStr);
}
請求標頭應為:
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.