我正在尝试从shopify API中提取多个产品,如此处文档中的curl请求所示:
curl -X GET "https://your-development-store.myshopify.com/admin/api/2023-
10/products.json?ids=632910392%2C921728736" \
-H "X-Shopify-Access-Token: {access_token}"
但是我正在使用 .NET Core 7,需要使用
HttpClient
并利用以下代码:
[HttpPost(Name = "PostDataExport")]
[EnableRateLimiting("api")]
public async Task<IActionResult> Post([FromBody] CustomerInquiry customerInquiry)
{
try
{
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get,
"https://mysite.myshopify.com/admin/api/2023-10/products.json?ids=7741485383833%7745763508377%7745765671065%7744839811225%7746517794969")
{
Headers =
{
{ "X-Shopify-Access-Token", _shopifyAPIAuth.AccessToken }
}
};
var httpClient = _httpClientFactory.CreateClient();
var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);
if (httpResponseMessage.IsSuccessStatusCode)
{
var content = await httpResponseMessage.Content.ReadAsStringAsync();
return Ok(shopifyResponse);
}
return Ok("failed");
}
catch (Exception ex)
{
_logger.LogInformation("Message: Error: " + ex.ToString(), DateTime.UtcNow.ToLongTimeString());
throw;
}
}
问题是这似乎只返回列表中的第一个产品 ID。如果我切换顺序,那么列表中新的第一个 id 就会被拉出。
我使用这个方法不正确吗?另外,有没有一种更简洁的方法来写出这样的参数,而不是直接输入网址?
您的 URL 似乎遇到了编码问题。
作为解决方法,而不是:
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get,
"https://mysite.myshopify.com/admin/api/2023-10/products.json?ids=7741485383833%7745763508377%7745765671065%7744839811225%7746517794969")
尝试使用绝对 URI:
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get,
new Uri("https://mysite.myshopify.com/admin/api/2023-10/products.json?ids=7741485383833%7745763508377%7745765671065%7744839811225%7746517794969").AbsoluteUri);