东莞市连易网络科技有限公司
东莞市连易网络科技有限公司
  • 网站
  • 用户
  • 购物车
  • 购物车
  • 搜索
  • 网站
  • 用户
帮助
帮助
开发文档 发起请求
  • 用户文档
  • 开发文档
  • API文档
  • 提及
  • 标签
  • 更多
  • 取消
  • 新建
  • +开始
  • +UI 自定义
  • -外部集成
    • -REST API
      • +REST SDK
      • 上传文件
      • 发起请求
      • 认证
    • Webhooks
    • +外部身份验证
  • +插件/框架扩展

发起请求

以下指南指导您使用 Limyee 电商平台 REST API 发起 REST 请求。我们建议始终通过 HTTPS 发出请求。为了防止身份验证头数据被截获,建议始终使用 HTTPS 向您的 Limyee 电商平台网站进行身份验证。

GET

GET 请求用于请求实体列表或显示一个实体的详细信息。GET 请求是最简单的 HTTP 方法。在此类请求中,所有数据都在 URL 中传递。理解 GET 请求的最简单方法是将其与在浏览器的地址栏中键入 URL 并单击"转到"时发生的情况进行比较:浏览器向该 URL 发出 GET 请求,检索网站返回的结果,并向您显示结果。发送到 Web 服务的 GET 请求的工作方式相同,只是请求通常通过代码执行。

下面是使用 REST 端点显示用户信息的 GET 请求示例。

// replace the "admin" and "Admin's API key" with your valid user and apikey!
var adminKey = String.Format("{0}:{1}", "Admin's API key", "admin");
var adminKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(adminKey));

var request = WebRequest.Create("https://mylimyeesite.com/api/v1/{0}.json",
    "username" // Username of the user we are returning
    ) as HttpWebRequest;
request.Method = "GET";
request.Headers.Add("Rest-User-Token", adminKeyBase64);

var response = request.GetResponse() as HttpWebResponse;
string text = null;
using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
}

Console.WriteLine(text);

POST

POST 请求将转换为 CREATE 请求。与 GET 请求不同,HTTP POST 请求在请求正文中包含其数据。如果您曾经在线提交过表单,则可能已提交了 HTTP POST 请求。如果不使用窗体、代码或浏览器插件,则无法直接从浏览器创建对 Web 服务的 POST 请求。

演示 POST 请求最简单方法是使用一些 C# 代码。以下代码片段演示如何向 Limyee 电商平台的用户端点提交 POST 请求。

下面是使用 REST 端点创建用户的 POST 请求示例。

// replace the "admin" and "Admin's API key" with your valid user and apikey!
var adminKey = String.Format("{0}:{1}", "Admin's API key", "admin");
var adminKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(adminKey));

var request = WebRequest.Create("https://mylimyeesite.com/api/v1/users.json") as HttpWebRequest;
request.Method = "POST";
request.Headers.Add("Rest-User-Token", adminKeyBase64);
request.ContentType = "application/x-www-form-urlencoded";

var data = string.Format("Username={0}&Password={1}&PrivateEmail={2}", 
    "username", // Username of the user we are creating
    "password", // Password of the user we are creating
    "username@mysite.com" // Private email address of the user we are creating
    );
byte[] bytes = Encoding.UTF8.GetBytes(data);

var requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();

var response = request.GetResponse() as HttpWebResponse;
string text = null;
using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
}

Console.WriteLine(text);

PUT

PUT 请求将转换为 UPDATE 请求。与 POST 请求一样,PUT 请求的数据包含在消息正文中。执行 PUT 请求与执行 POST 请求非常相似。要更新 ID 为 2000 的用户,您需要使用类似于上述 POST 示例的代码对 http://localhost/api/v1/users/2000.xml 端点执行 PUT 请求。

出于安全目的,某些 Web 服务器(包括 IIS)默认禁用 PUT 和 DELETE 请求。因此,Limyee 电商平台的 REST 服务为 PUT 操作提供了 POST 方法的重载,允许您通过使用自定义 HTTP 头(Rest-Method)发出 POST 请求来执行类似 PUT 的功能 。

下面是使用 REST 端点更新用户的 PUT 请求示例。

// replace the "admin" and "Admin's API key" with your valid user and apikey!
var adminKey = String.Format("{0}:{1}", "Admin's API key", "admin");
var adminKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(adminKey));

var request = WebRequest.Create(string.Format("https://mylimyeesite.com/api/v1/users/{0}.json", 
    "username" // Username of the user we are updating
    )) as HttpWebRequest;
request.Method = "PUT";

// OR
// request.Method = "POST";
// request.Headers.Add("Rest-Method", "PUT");

request.Headers.Add("Rest-User-Token", adminKeyBase64);
request.ContentType = "application/x-www-form-urlencoded";

// We are only updating the users Private Email
var data = string.Format("PrivateEmail={0}", "username.updated@mysite.com");
byte[] bytes = Encoding.UTF8.GetBytes(data);

var requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();

var response = request.GetResponse() as HttpWebResponse;
string text = null;
using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
}

Console.WriteLine(text);

DELETE

用于删除请求。如果向 http://localhost/api/v1/users/2000.xml 发出 DELETE 请求,则要求删除 ID 为 2000 的用户。

出于安全目的,某些 Web 服务器(包括 IIS)默认禁用 PUT 和 DELETE 请求。因此,Limyee 电商平台 REST 服务为 DELETE 操作提供了 POST 方法的重载,允许您通过使用自定义 HTTP 头(Rest-Method)发出 POST 请求来执行类似 DELETE 的功能 。

下面是使用 REST 端点删除用户的 DELETE 请求示例。

// replace the "admin" and "Admin's API key" with your valid user and apikey!
var adminKey = String.Format("{0}:{1}", "Admin's API key", "admin");
var adminKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(adminKey));

var request = WebRequest.Create(string.Format("https://mylimyeesite.com/api/v1/users/{0}.xml?includetypes=true", 
    "username" // Username of the user we are deleting
    )) as HttpWebRequest;
request.Method = "DELETE";

// OR
// request.Method = "POST";
// request.Headers.Add("Rest-Method", "DELETE");

request.Headers.Add("Rest-User-Token", adminKeyBase64);

request.ContentLength = 0;

var response = request.GetResponse() as HttpWebResponse;
string text = null;
using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
}

Console.WriteLine(text);

使用批处理 REST 端点

BATCH REST 端结点可用于在批处理中发出一系列 REST 请求。一个批处理请求最多可以发出 100 个请求。批处理请求也可以设置为按顺序运行。如果一个请求失败,则按顺序运行时,将不会执行剩余的请求。使用此端点可以通过减少外部应用程序和 Limyee 电商平台之间的跳动以及允许请求同时运行,以提高性能。

以下示例使用 BATCH REST 端点在一个请求中同时创建用户和群组。

// replace the "admin" and "Admin's API key" with your valid user and apikey!
var adminKey = String.Format("{0}:{1}", "Admin's API key", "admin");
var adminKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(adminKey));

var request = WebRequest.Create("https://mylimyesite.com/api/v1/batch.json") as HttpWebRequest;
request.Method = "POST";
request.Headers.Add("Rest-User-Token", adminKeyBase64);
request.ContentType = "application/x-www-form-urlencoded";

var userData = string.Format("Username={0}&Password={1}&PrivateEmail={2}", 
    "username", // Username of the user we are creating
    "password", // Password of the user we are creating
    "username@mysite.com"); //Private email addres of the user we are creating

var groupData = string.Format("Name={0}&GroupType={1}", 
    "name", // Name of the group we are creating
    "Joinless"); // Type of group we are creating

var batchData =
    string.Format(
        "_REQUEST_0_URL={0}&_REQUEST_0_METHOD={1}&_REQUEST_0_DATA={2}&_REQUEST_1_URL={3}&_REQUEST_1_METHOD={4}&_REQUEST_1_DATA={5}",
        HttpUtility.UrlEncode("~/api/v1/users.json"), 
        "POST", 
        HttpUtility.UrlEncode(userData), 
        HttpUtility.UrlEncode("~/api/v1/groups.json"), 
        "POST", 
        HttpUtility.UrlEncode(groupData));

byte[] bytes = Encoding.UTF8.GetBytes(batchData);

var requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();

var response = request.GetResponse() as HttpWebResponse;
string text = null;
using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
}

Console.WriteLine(text);

使用上面的示例,将 &Sequential=true 添加到 batchData 查询字符串可以使请求按顺序运行。

var batchData =
string.Format(
    "_REQUEST_0_URL={0}&_REQUEST_0_METHOD={1}&_REQUEST_0_DATA={2}&_REQUEST_1_URL={3}&_REQUEST_1_METHOD={4}&_REQUEST_1_DATA={5}&Sequential=true",
    HttpUtility.UrlEncode("~/api/v1/users.json"), 
    "POST", 
    HttpUtility.UrlEncode(userData), 
    HttpUtility.UrlEncode("~/api/v1/groups.json"), 
    "POST", 
    HttpUtility.UrlEncode(groupData));

使用脚本 REST 端点

脚本 REST 端点是用于通过 REST 执行小组件。代码示例是小组件使用 GET 和 POST 请求。脚本 REST 端点提供了能够在一个 REST API 调用中进行多个 API 调用的性能优势。它还允许您通过仅包含您真正需要的字段来减小 HTTP 响应的大小。它还允许预先呈现响应,并且可以返回所需的任何数据类型:HTML,CSV,XML,JSON等。

GET

通过转到管理>界面>小组件来创建新的小组件。创建小组件后,您将看到概述页面,您可以在其中单击"显示详细信息"链接以获取与脚本 REST 端点一起使用的小组件 ID 值。要了解有关小组件的更多信息,请参阅本指南的小组件部分。

以下示例小组件接受 QueryString 参数 ContentId 和 ContentTypeId,并将返回 JSON 响应,该响应将包括所有评论以及在评论上的任何赞及ContentId 和 ContentTypeId。

$limyee_v1_page.SetContentType('application/json')

#set ($ContentId = $limyee_v1_utility.ParseGuid($limyee_v1_page.GetQueryStringValue('ContentId')))
#set ($ContentTypeId = $limyee_v1_utility.ParseGuid($limyee_v1_page.GetQueryStringValue('ContentTypeId')))

#if (!$limyee_v1_page.IsPost && $ContentId && $ContentTypeId)
#set($comments = $limyee_v1_comments.List("%{ContentId = $ContentId, ContentTypeId = $ContentTypeId}"))
    {"Comments": [
        #foreach($comment in $comments)
            #before
                {
            #each
            "Comment":{
                #set($likes = $limyee_v1_like.List("%{ContentId = $comment.CommentId, ContentTypeId = $limyee_v1_comments.ContentTypeId}"))
                #if ($likes.HasErrors())
    		        $limyee_v1_page.SendJsonError($likes.Errors)
    		    #else
                    "ContentId": "$comment.ContentId","ContentTypeId": "$comment.ContentTypeId","Body": "$comment.Body","UserId": "$comment.UserId","Likes": [
                            #foreach($like in $likes)
                                #before
                                {
                                #each
                                    "Like":{"UserId":"$like.UserId","CreatedDate":"$like.CreatedDate"}
                                #after
                                }
                            #end
                            ]
            #after
                }
                #end
            }
        #end
    ]}
#end

POST

通过转到管理>界面>小组件来创建新的小组件。创建小组件后,您将看到概述页面,您可以在其中单击"显示详细信息"链接以获取与脚本 REST 端点一起使用的小组件 ID 值。要了解有关小组件的更多信息,请参阅本指南的小组件部分。

以下示例小组件传入 QueryString 参数 ContentId 和 ContentTypeId,并对关联的内容创建赞。响应将以 JSON 格式返回。

$limyee_v1_page.SetContentType('application/json')

#set ($ContentId = $limyee_v1_utility.ParseGuid($limyee_v1_page.GetQueryStringValue('ContentId')))
#set ($ContentTypeId = $limyee_v1_utility.ParseGuid($limyee_v1_page.GetQueryStringValue('ContentTypeId')))

#if ($limyee_v1_page.IsPost && $ContentId && $ContentTypeId)
    #set($likeResponse = $limyee_v1_like.Create($ContentId, $ContentTypeId))
    {"Like": {"UserId":"$likeResponse.UserId","ContentId":"$likeResponse.ContentId","CreatedDate":"$likeResponse.CreatedDate"}}
#end

以下示例 POST 请求使用脚本化 REST 端点调用小组件。

// replace the "admin" and "Admin's API key" with your valid user and apikey!
var adminKey = String.Format("{0}:{1}", "Admin's API key", "admin");
var adminKeyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(adminKey));

var request = WebRequest.Create(string.Format("https://mylimyeesite.com/api/v1/scripted?Id={0}&ContentId={1}&ContentTypeId={2}",
    "d74cc0b2-fec0-485e-a7bf-9f1ffd60414d", //Widget ID of the widget we are calling
    "811f98af-ef88-4539-913f-8deadf401527", //ContentId of the item we are creating a like on
    "f3f09990-77aa-4450-ba75-9dbedb76831e" //ContentTypeId of the item we are creating a like on
   )) as HttpWebRequest;
request.Method = "POST";
request.Headers.Add("Rest-User-Token", adminKeyBase64);
request.ContentLength = 0;

var response = request.GetResponse() as HttpWebResponse;
string text = null;
using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
}

Console.WriteLine(text);

  • 分享
  • 历史
  • 更多
  • 取消
相关
推荐
Copyright © 2021 东莞市连易网络科技有限公司. All rights reserved.
Powered by Limyee Commerce System(3.0.0). © 2014 - 2025 Limyee Inc.