Skip to content

Using HTTPS for REST calls #278

Description

@DanujaSW

Hi,

I have set up the function below to send a HTTP GET request to a REST endpoint. May I know if libulfius supports REST calls via HTTPS?

If so, may I have some guidance on how this can be implemented?

Thanks

Function:

char* get_update(void) {
	struct _u_request request;
	struct _u_response response;
	json_t *json_response;
	char *json_str = NULL;
	
	//Initialize the request and response structures
	ulfius_init_request(&request);
	ulfius_init_response(&response);
	
	//Set the HTTP method and URL
	request.http_verb = strdup("GET");
	request.http_url = strdup(UPDATE_URL);
	
	//send the HTTP request
	if (ulfius_send_http_request(&request, &response) == U_OK) {
		if (response.status == 200) {
			json_error_t error;
			json_response = ulfius_get_json_body_response(&response, &error);
			if (json_response) {
				//obtain the JSON response
				json_str = json_dumps(json_response, JSON_INDENT(2));
				//Decrese reference count of JSON object to free memory
				json_decref(json_response);
			}
			else {
				fprintf(stderr,"Error parsing JSON response: %s\n", error.text);
				json_str = "ERROR";
			}
		}
		else {
			fprintf(stderr, "HTTP request failed with status %d\n", response.status);
		}
	}
	else {
		json_str = "ERROR";
		fprintf(stderr, "Error sending HTTP request\n");
	}
	
	//Clean up 
	ulfius_clean_request(&request);
	ulfius_clean_response(&response);
	
	//Free the memory allocated by strdup
	free(request.http_verb);
	free(request.http_url);
	
	return json_str;
	
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions