简体   繁体   English

编解码与Microsoft SQL Server using Postman

[英]Encoding and decoding with Microsoft SQL Server using Postman

I wrote the following script to be used through Web API:我写了下面的脚本通过Web API使用:

CREATE PROCEDURE [dbo].[LWP_WEB_ENCODE]
    @order_no int, 
    @dept varchar(2)
    
    DECLARE @customer_no int 
    SET @customer_no = (SELECT customer_no FROM table_order 
                        WHERE order_no = @order_no)
        
    DECLARE @string varchar (4000), @encoded VARBINARY(max)

    SELECT @string = CAST(@order_no AS varchar) + '-' + 
                     CAST(@customer_no AS varchar) + '-' + @dept_abbreviation
            
    SELECT @encoded = CONVERT(VARBINARY(MAX), @string)
    SELECT @encoded AS 'encoded'

When I execute the query with the following paramters:当我使用以下参数执行查询时:

exec [LWP_WEB_LWP_WEB_ENCODE] @order_no = 7267587, @dept_abbreviation = 'S'

I get the following string back我得到以下字符串

在此处输入图像描述

I then do the same thing through postman in an effort to simulate the web calls and get something completely different然后我通过 postman 做同样的事情来模拟 web 调用并得到完全不同的东西

在此处输入图像描述

I'm not sure why its behaving like this - but also I have a script that decodes the string in the opposite direction and this particular value doesn't work我不确定为什么它会这样 - 但我也有一个脚本可以在相反方向解码字符串并且这个特定值不起作用

The decoded procedure basically just runs this解码程序基本上只是运行这个

SELECT CONVERT(VARCHAR(MAX), @encoded) AS 'decoded'

When I run my script当我运行脚本时

exec [LWP_WEB_DECODE] @encoded = 0x373236373538372D38373132323938372D53

I get this in SQL我在 SQL 得到这个

在此处输入图像描述

However then I try to run the value returned in postman it hates it For some reason its not encoding it as I do in SQL and its returning a string and not a hex value但是然后我尝试运行 postman 中返回的值它讨厌它出于某种原因它没有像我在 SQL 中那样对其进行编码并且它返回一个字符串而不是十六进制值

在此处输入图像描述

The stored procedure returns binary data.存储过程返回二进制数据。 SSMS displays binary data by encoding it in a hex string. SSMS 通过将二进制数据编码为十六进制字符串来显示二进制数据。 JSON and XML can't directly contain binary data, but most often use Base64 to encode it as a string rather than using a hex string. JSON 和 XML 不能直接包含二进制数据,但最常使用Base64将其编码为字符串,而不是使用十六进制字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM