【问题标题】:Getting 422 Status Code Error When Posting to Square?发布到 Square 时出现 422 状态码错误?
【发布时间】:2016-05-05 21:29:33
【问题描述】:

请帮我完成最后一步,这样我才能完成这个项目:)

我正在尝试将此示例转换为 Vb.Net:

https://docs.connect.squareup.com/articles/processing-payment-rest/

用户输入信用卡信息后,我可以成功获得 CardOnce ID。我可以成功地从 Square 读取以获取我的位置 ID。但是,当我提供 CardOnce ID、位置 ID 和金额到 POST 时,我卡在最后一步,我不断收到错误消息:“远程服务器返回错误:(422) 状态代码 422。”

下面列出了我的代码和错误发生:

调用时发生错误:

response = DirectCast(request.GetResponse(), HttpWebResponse)

完整代码在这里:

Public Function IsSuccessProcess(ByVal sLocationId As String, ByVal sCardOnce As String, ByVal iAmount As Integer)
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim reader As StreamReader
    Dim address As Uri
    Dim data As StringBuilder
    Dim byteData() As Byte
    Dim postStream As Stream = Nothing

    address = New Uri("https://connect.squareup.com/v2/locations/" & sLocationId & "/transactions")

    ' Create the web request  
    request = DirectCast(WebRequest.Create(address), HttpWebRequest)

    ' Set type to POST  
    request.Method = "POST"
    request.ContentType = "application/json"
    request.Accept = "application/json"
    request.Headers.Add("Authorization", "Bearer " & AccessToken)

    data = New StringBuilder()
    Dim sQuote As String = """"
    sQuote = "'"
    data.Append("{" & sQuote & "card_nonce" & sQuote & ": " & sCardOnce & "," & sQuote & "amount_money" & sQuote & ": {" & sQuote & "amount" & sQuote & ": " & iAmount & "," & sQuote & "currency" & sQuote & ": " & sQuote & "USD" & sQuote & "}")

    Dim didempotency_key As Double = Microsoft.VisualBasic.Timer
    Dim idempotency_key As Integer = CInt(didempotency_key)
    data.Append("'idempotency_key': " & idempotency_key)
    ' Create a byte array of the data we want to send  
    byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())
    ' Set the content length in the request headers  
    request.ContentLength = byteData.Length

    ' Write data  
    Try
        postStream = request.GetRequestStream()
        postStream.Write(byteData, 0, byteData.Length)
    Finally
        If Not postStream Is Nothing Then postStream.Close()
    End Try

    Try
        ' Get response  
        response = DirectCast(request.GetResponse(), HttpWebResponse)

        ' Get the response stream into a reader  
        reader = New StreamReader(response.GetResponseStream())

        ' Console application output  
        Console.WriteLine(reader.ReadToEnd())
    Finally
        If Not response Is Nothing Then response.Close()
    End Try


    Return True

End Function

【问题讨论】:

  • HTTP 响应的正文中可能有一个更具体的错误 - 你能打印出来吗?
  • 我正在尝试,但我不确定我找对地方了。当我尝试获得响应时,它会触发错误,因此“响应”变量为空。我可以检查“请求”变量的部分内容。我应该尝试查看一些特定的变量或命令吗?
  • 我想通了。我的 json 字符串并不完全正确。我将在我的答案中包含正确的版本。那根弦太喜怒无常了:/ ...但一切都很好,结局很好。不过,我仍然想知道如何查看该 HTTP 响应。

标签: asp.net vb.net square-connect


【解决方案1】:

此错误很可能发生,因为您在 JSON 正文中使用单引号而不是双引号。第一步尝试替换它们。

请注意,您可能会发现借助 Json.NET 等第三方库更容易生成和解析 JSON 对象。

最后,正如 Troy 所提到的,有用的错误消息会在端点响应正文中返回,这应该可以帮助您诊断未来的问题。有关更多信息,请参阅this article

【讨论】:

  • 感谢您的回复。我希望这些类型的问题得到更多的关注。实际上,我确实尝试了单引号(撇号)和双引号(引号),但都没有奏效——事实上,在我的代码中,您可以看到单撇号上方的行默认设置为我首先尝试的双引号。如果我必须使用第三方,我会,但我通常不愿意这样做。我目前不在项目中,但我会尝试看看并回复特洛伊。如果不是太难,也许我也会尝试第三方应用程序。再次感谢。
  • 我想通了。我的 json 字符串并不完全正确。我终于找到了获胜的组合。我会把它包括在答案中。
【解决方案2】:

我一直在玩 JSON 字符串,最终获得了获胜组合。我没有正确使用“amount_money”。此外,SDK 说你必须使用我在一些试验中没有使用的 idempotency_key。最后,您必须使用实际引号,而不是单个撇号:

Dim sKey As String = Guid.NewGuid().ToString()
Dim sMyJsonString As String
sMyJsonString = "{" & sQuote & "card_nonce" & sQuote & ":" & sQuote & sCardOnce & sQuote & "," & sQuote & "amount_money" & sQuote & ": {" & sQuote & "amount" & sQuote & ": " & iAmount & "," & sQuote & "currency" & sQuote & ": " & sQuote & "USD" & sQuote & "}," & sQuote & "idempotency_key" & sQuote & ":" & sQuote & sKey & sQuote & "}"
data.Append(sMyJsonString)

我最终向自己收取了 1.00 美元,显示在我的 Square 帐户中。这比我希望的要花更长的时间来弄清楚,但一切都很好,结局很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多