When testing and debugging the policy code and running the robot in real operation, the exchange interface error is often encountered. At this time, the exchange interface API document is queried to query relevant error information, and the exchange API technical customer service always needs to provide the request message when the error is reported to analyze the error cause. At this time, if you can’t see the message information, you can’t start to find the problem. In this article, we will discuss two solutions.

1. Use Python’s SCAPy library to capture and print the sent request message

First install the SCAPy module

pip3 install scapy 
Copy the code

Then create a Python policy:

from scapy.all import *

def Method_print(packet):
    ret = "\n".join(packet.sprintf("{Raw:%Raw.load%}").split(r"\r\n"))
    Log(ret)

sniff(
    iface='eth0',
    prn=Method_print,
    lfilter=lambda p: "GET" in str(p) or "POST" in str(p),
    filter="tcp")
Copy the code

Then create a robot that uses this policy, and the robot will grab HTTP packets issued by the host’s server (HTTPS catch failure has some handling for this). Run the packet capture robot, and then use the debugging tool to send a request for the robot to capture packets. In the debug tool we write the code that sends the request.

Function main(){// To set the base address to another HTTP protocol address, if you do not set the exchange address is usually HTTPS, Exchange.SetBase("http://www.baidu.com") // POST request exchange.IO(" API ", "POST", "/ API /swap/v3/order", SetContractType("swap") exchange.getticker ()} "aaa=111& BBB =222")Copy the code

Information printed by the bag capturing robot:

We can copy the message: GET request message:

GET/API/swap/v3 / instruments/BTC - USD - swap/ticker HTTP / 1.1 Host: www.baidu.com the user-agent: Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 Accept-encoding: gzipCopy the code

Host: www.baidu.com www.okex.com you can see the links in the request message is: / API/swap/v3 / instruments/BTC – USD – swap/ticker, is to ask BTC standard sustainable contract market data.

POST Request message:

POST/API /swap/v3/order HTTP/1.1 Host: www.baidu.com user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 Content-Length: 25 Content-Type: application/json; charset=UTF-8 Ok-Access-Key: d487230f-ccccc-aaaaa-bbbbb-268fef99cfe4 Ok-Access-Passphrase: abc123 Ok-Access-Sign: H1x6f80rhhkELobJcO1rFyMgUUshOlmgjRBHD + pLvG0 = Ok - Access - Timestamp: the 2020-09-23 T08:43:49. Accept 906 z - Encoding: gzip {"aaa":"111","bbb":"222"}Copy the code

Request path: / API /swap/v3/order. Access key: d487230F-ccCCC-AAAAA-bbbbb-268FEf99cfe4 H1x6f80rhhkELobJcO1rFyMgUUshOlmgjRBHD + pLvG0 = API KEY secret KEY Passphrase: abc123 (demonstration) request Body data: {” aaa “:” 111 “, “BBB” : “222”}.

In this way, you can observe the request packets and analyze the cause of the interface request error.

2. Local listening requests

The second method, do not need to create a robot, apple Mac used to own Netcat: baike.baidu.com/item/Netcat… . Listen for requests and print messages.

On the terminal, run the nC-L 8080 command to start Netcat.

As shown in figure:

Again we deploy a host on the native machine and send the request in the debugger using the following code.

Function main(){exchange.setbase ("http://127.0.0.1:8080") Exchange.IO(" API ", "POST", "/ API /swap/v3/order", SetContractType("swap") exchange.getticker ()} "aaa=111& BBB =222")Copy the code

POST request packets printed on the terminal:

GET request packets printed on the terminal: