Skip to content

Commit

Permalink
Multipart request
Browse files Browse the repository at this point in the history
  • Loading branch information
abobija committed Aug 14, 2022
1 parent dc14732 commit 0cdcd6b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions include/discord/private/_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern "C" {
#include "esp_http_client.h"
#include "discord.h"

#define DCAPI_REQUEST_BOUNDARY "esp-discord"

#define DCAPI_POST(strcater, serializer, stream) ({ \
char* _uri = strcater; \
char* _json = serializer; \
Expand Down
29 changes: 22 additions & 7 deletions src/discord/private/_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ static esp_err_t dcapi_init_lazy(discord_handle_t client, bool download, const c
// todo: error check
free(auth);

esp_http_client_set_header(client->http, "Content-Type", "application/json");
esp_http_client_set_header(client->http, "Content-Type", "multipart/form-data; boundary=\"" DCAPI_REQUEST_BOUNDARY "\"");
// todo: error check
}

Expand Down Expand Up @@ -220,8 +220,19 @@ static esp_err_t dcapi_request(discord_handle_t client, discord_api_request_t* r
esp_http_client_set_method(http, request->method);
// todo: error check

const char* payload_boundary = "--" DCAPI_REQUEST_BOUNDARY
"\nContent-Disposition: form-data; name=\"payload_json\""
"\nContent-Type: application/json"
"\n\n";

const char* multipart_end = "\n--" DCAPI_REQUEST_BOUNDARY "--";

int len = request->payload ? strlen(request->payload) : 0;

if(len > 0) {
len += strlen(payload_boundary) + strlen(multipart_end);
}

bool connection_open = false;
const uint8_t open_attempts = 3;
uint8_t open_attempt = 0;
Expand All @@ -242,12 +253,16 @@ static esp_err_t dcapi_request(discord_handle_t client, discord_api_request_t* r
}

if(request->payload) {
DISCORD_LOGD("Writing data to request:\n%.*s", len, request->payload);

if(esp_http_client_write(http, request->payload, len) == ESP_FAIL) {
DISCORD_LOGW("Fail to write data to request");
xSemaphoreGive(client->api_lock);
return ESP_FAIL;
DISCORD_LOGD("Writing payload to request:\n%.*s", len, request->payload);

if(len > 0) {
if(esp_http_client_write(http, payload_boundary, strlen(payload_boundary)) == ESP_FAIL
|| esp_http_client_write(http, request->payload, strlen(request->payload)) == ESP_FAIL
|| esp_http_client_write(http, multipart_end, strlen(multipart_end)) == ESP_FAIL) {
DISCORD_LOGW("Fail to write data to request");
xSemaphoreGive(client->api_lock);
return ESP_FAIL;
}
}

if(! request->disable_auto_payload_free) { free(request->payload); }
Expand Down

0 comments on commit 0cdcd6b

Please sign in to comment.