networking · beginner · ~15 min

Valid HTTP method?

Validate against the common HTTP methods.

Challenge

Check whether a string is one of the common HTTP methods a server accepts.

Task

Implement int is_http_method(const char *m).

Input

  • m: a NUL-terminated candidate method string.

Output

Return 1 if m is exactly one of GET, POST, PUT, DELETE, or HEAD (case-sensitive), else 0.

Example

is_http_method("GET")    ->  1
is_http_method("POST")   ->  1
is_http_method("FETCH")  ->  0

Input format

m: a NUL-terminated candidate method string.

Output format

1 if m is one of GET, POST, PUT, DELETE, HEAD; else 0.

Constraints

Exact, case-sensitive match against the five-method set.

Starter code

#include <string.h>

int is_http_method(const char *m) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.