Home > Net >  C HTTP 2 POST request getting rejected
C HTTP 2 POST request getting rejected

Time:02-06

I am trying to simulate this Curl command but only using C :

curl -H "X-MBX-APIKEY: dummy_one" -X POST 'https://api.binance.com/api/v3/order?hello'

The above request generates this response:

{"code":-2014,"msg":"API-key format invalid."}

Curl's verbose logging switch shows it sent this:

> POST /api/v3/order?hello HTTP/2
> Host: api.binance.com
> user-agent: curl/7.81.0
> accept: */*
> x-mbx-apikey: dummy_one
> 

This is my attempt in C :

char* write_buf = "POST /api/v3/order HTTP/2\r\n"
                  "Host: api.binance.com\r\n"
                  "user-agent: curl/7.81.0\r\n"
                  "accept: */*\r\n"
                  "X-MBX-APIKEY: dummy_one\r\n"
                  "content-length: 5\r\n"
                  "content-type: application/x-www-form-urlencoded\r\n"
                  "hello";

if(BIO_write(bio, write_buf, strlen(write_buf)) <= 0)
{

However, my request is getting rejected with:

HTTP/1.1 505 HTTP Version Not Supported

After Googling I think this could be a generic error response, particularly with un-escaped spaces.

I'm not sure what is the exact problem.

My SSL connection is fine, it works perfectly with a GET request. The problem appears to be in the format of the POST request message.

CodePudding user response:

This is HTTP/1.1. HTTP/2 is a binary protocol. Just change your /2 to /1.1 and you should get unstuck. HTTP/1.1 is not deprecated.

Curl verbose logging doesn't show the wire protocol, but rather just what it semantically sent.

  • Related