Search code examples
c++jsonpostmultipart

Properly formatting POST with multipart json in C


I'm trying to make a POST request using C because I need my executable to be small and non-dependent on .NET or other libraries.

When executing the code below, the server gets NULL value for both expected json strings:

void MakePostReq1(
    const char* verb,
    const char* hostname,
    int port,
    const char* resource,
    const char* opt_urlencoded,
    string& response)
{
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        cout << "WSAStartup failed.\n";
        exit(1);
    }

    SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    struct hostent *host;
    host = gethostbyname(hostname);

    SOCKADDR_IN SockAddr;
    SockAddr.sin_port = htons(port);
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);

    cout << "Connecting...\n";

    if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0)
    {
        cout << "Could not connect";
        exit(1);
    }
    cout << "Connected.\n";

    string json1 = "";
    json1.append("------------------------e661208454117a87");
    json1.append("\r\n");
    json1.append("Content-Disposition: form-data; name=\"jsonObject\"\r\n");
    json1.append("Content-Type: application/json\r\n");
    json1.append("\r\n");
    json1.append("{ jsonObject: \"This is a jsonObject\" }");
    json1.append("\r\n");
    json1.append("\r\n");

    string json2 = "";
    json2.append("------------------------e661208454117a87");
    json2.append("\r\n");
    json2.append("Content-Disposition: form-data; name=\"jsonEventObject\"\r\n");
    json2.append("Content-Type: application/json\r\n");
    json2.append("\r\n");
    json2.append("{ jsonEventObject: \"This is a jsonEventObject\" }");
    json2.append("\r\n");
    json2.append("\r\n");

    string trailer = "";
    trailer.append("------------------------e661208454117a87");
    trailer.append("\r\n");


    // Build request
    string req = "POST"; // GET | POST
    req.append(" ");
    req.append(resource);
    req.append(" HTTP/1.1\r\n");
    req.append("Host: ");
    req.append(hostname);
    req.append(":");
    req.append(to_string(port));
    req.append("\r\n");

    req.append("Cache-Control: no-cache\r\n");
    req.append("Content-length: ");
    char size[5];
    sprintf(size, "%d", json1.size() + json2.size() + trailer.size());
    //sprintf(size, "%d", 319);
    req.append( size );
    req.append("\r\n");
    req.append("Content-Type: multipart/form-data; boundary=\"------------------------e661208454117a87\"\r\n\r\n");

    // add json strings
    req.append(json1);
    req.append(json2);
    req.append(trailer);

    cout << endl;

    cout << "Request size: " << req.size() << endl;
    cout << "Request " << endl << req << endl;

    send(Socket, req.c_str(), req.size(), 0);

    closesocket(Socket);
    WSACleanup();
} 

Solution

  • On this line:

    req.append("Content-Type: multipart/form-data; boundary=\"------------------------e661208454117a87\"\r\n\r\n");
    

    Remove the quotes from the boundary:

    req.append("Content-Type: multipart/form-data; boundary=------------------------e661208454117a87\r\n\r\n");
    

    The quotes are interpreted as being a part of the boundary, thus the data boundaries you have below aren't recognized.