使用VB.NET调用Smartlane REST API查物流轨迹遇422错误的解决方法
解决REST API物流跟踪接口的422无效负载错误
错误信息
{"code":422,"status":"failure","message":"Invalid Payload! Please check the api documentation or contact at dev@smartlane.pk.","errors":{"store_order_id":["The store order id field is required."]}}
问题分析
- GET请求不应携带请求体:HTTP规范中GET请求通常不包含请求体,多数API不会解析GET请求的Body内容,导致你的
store_order_id参数未被正确识别。 - 参数格式可能不符:代码中将
store_order_id设为数组格式["AS41929"],但API可能期望单个字符串值。
解决方案
根据API常见设计规范,提供两种修正方案:
方案一:改用POST请求传递请求体
如果API要求通过请求体传递参数,将请求方法改为POST,并调整参数格式:
Dim client = New RestClient("https://v2.Smartlane.dev/api/consignment/track") client.Timeout = -1 ' 将请求方法改为POST Dim request = New RestRequest(Method.POST) request.AddHeader("Authorization", "Bearer -------------------------------") request.AddHeader("Content-Type", "application/json") ' 将store_order_id改为单个字符串(匹配API预期格式) Dim body = "{""store_order_id"": ""AS41929""}" request.AddParameter("application/json", body, ParameterType.RequestBody) Dim response As IRestResponse = client.Execute(request) Console.WriteLine(response.Content)
方案二:用GET请求的查询参数传递ID
如果API支持通过URL查询参数传递store_order_id,直接添加查询参数:
Dim client = New RestClient("https://v2.Smartlane.dev/api/consignment/track") client.Timeout = -1 Dim request = New RestRequest(Method.GET) request.AddHeader("Authorization", "Bearer -------------------------------") ' 添加查询参数传递store_order_id request.AddQueryParameter("store_order_id", "AS41929") Dim response As IRestResponse = client.Execute(request) Console.WriteLine(response.Content)
额外建议
- 优先查阅API官方文档,明确接口支持的请求方法(GET/POST)和参数传递方式(查询参数/请求体)。
- 若API确实要求GET请求带请求体,可尝试升级RestSharp版本,或确认数组格式是否为API预期格式。
内容的提问来源于stack exchange,提问作者Samee Ullah
相关产品推荐
相关产品推荐

