1

I want to send some binary data to a php http server via HTTP POST method from a pic32 embedded board . my server will reply with data which he will receive. I get 0x20 instead of 0x2b ('+') from server.

my function is

void send_post_query(void)
{
        char *hname = "www.example.com";
        char *page = "/test/testAES.php";
        char *poststr = "status=";
        char * data_string ="This is to be encrypted before sending";
        unsigned char encoded_data[512];
        unsigned int encoded_data_length;
        char status_string[512];
        unsigned char data_string[512];
        unsigned char header_lenght=0;

        //Encode string 
        encoded_data_length= EncryptSendingPacket(data_string,strlen(data_string),encoded_data);

        //create query
        snprintf(status_string, 512,
         "POST %s HTTP/1.1\r\n"
         "Host: %s\r\n"
         "Content-type: application/x-www-form-urlencoded\r\n"
         "Content-length: %d\r\n\r\n"
                 "%s", page, hname, encoded_data_length+strlen(poststr),poststr);
        header_lenght=strlen(status_string);
        //Append data through string
        memcpy(status_string+header_lenght,encoded_data,encoded_data_length);
        //Send data through an open socket
        send(ulSocket, status_string, header_lenght+encoded_data_length,0);

}

I found some post regarding the usage of 'Content-Type': 'application/octet-stream'. but on changing this ,not even any byte received from server. (simply i don't know how to use 'Content-Type': 'application/octet-stream' from c ).Please help me to fill this http post header for sending binary data.

Any help will be thankful.

1 Answer 1

2

One problem I see is that you're copying your binary data (unsigned char) into a "string" (char) variable.

The easiest way I see is to issue two send() calls:

  • 1 for the header data
  • 1 for the binary data (body)

Also, you'll need to set your content type to "application/octet-stream". A content type of "x-www-form-urlencoded" is completely wrong for binary data.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply... When i used "application/octet-stream" , php server is not recognizing $_REQUEST['status’] command. But above incorrect code detects status but 0x20 instead of 0x2b. What will be the reason?
If I read your code correctly, the data after the "status=" will be your encrypted data. So, you're putting two different types of data together without telling the receiver where the boundaries are for the data. You're going to need to set up your message body to be proper MIME format. See MIME
"One problem I see is that you're copying your binary data (unsigned char) into a "string" (char) variable." This is not a problem, unless you perform operations on it which care about the sign bit, on a platform where char is signed.
@ChrisStratton, good catch! I didn't even realize that. Also, the sizes could be an issue. Copying a 512 byte data block plus header into a 512 byte block...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.