Skip to content

CefRequest

Header: cef_request.h
Interfaces: CefRequest, CefPostData, CefPostDataElement
Category: Core APIs

Overview

Foundational interfaces: process bootstrap, browser lifecycle, frames, DOM, requests, and data objects.

CefRequest

Source File: include/cef_request.h

Process / Thread Context: Any thread (per class comment: "The methods of this class may be called on any thread.") — except GetResourceType and GetTransitionType which are documented as browser-process only.

Purpose: USER-CALLED library interface representing a web request. Used by CefResourceRequestHandler, CefResourceHandler, CefURLRequest, and various browser callbacks.

Methods

static CefRefPtr<CefRequest> Create()

Return Value: Create a new mutable request. Any thread.

Threading Constraint: Threading constraint not specified in source.

bool IsReadOnly()

Return Value: True if the request cannot be modified (e.g., supplied to read-only callbacks).

Threading Constraint: Threading constraint not specified in source.

void SetReferrer(const CefString& referrer_url, ReferrerPolicy policy)

Return Value: Set referrer URL and policy. Non-empty URL must be HTTP/HTTPS-scheme fully qualified; username/password/fragment stripped.

Threading Constraint: Threading constraint not specified in source.

CefString GetReferrerURL()

Return Value: Current referrer URL.

Threading Constraint: Threading constraint not specified in source.

ReferrerPolicy GetReferrerPolicy()

Return Value: Defaults to REFERRER_POLICY_DEFAULT.

Threading Constraint: Threading constraint not specified in source.

void GetHeaderMap(HeaderMap& headerMap)

Return Value: Get all headers. Does NOT include the Referer value.

Threading Constraint: Threading constraint not specified in source.

void SetHeaderMap(const HeaderMap& headerMap)

Return Value: Replace all headers. Existing Referer is removed and ignored.

Threading Constraint: Threading constraint not specified in source.

CefString GetHeaderByName(const CefString& name)

Return Value: First header value for name or empty. Does not return the Referer value; use GetHeaderMap if name may have multiple values.

Threading Constraint: Threading constraint not specified in source.

void SetHeaderByName(const CefString& name, const CefString& value, bool overwrite)

Return Value: Set header. Referer cannot be set with this method.

Threading Constraint: Threading constraint not specified in source.

void Set(const CefString& url, const CefString& method, CefRefPtr<CefPostData> postData, const HeaderMap& headerMap)

Return Value: Set URL, method, post data, and headers in one call. postData is optional.

Threading Constraint: Threading constraint not specified in source.

ResourceType GetResourceType()

Return Value: Returns the resource type. Default RT_SUB_RESOURCE. Only available in the browser process.

Threading Constraint: Threading constraint not specified in source.

TransitionType GetTransitionType()

Return Value: Returns transition type (applies only to main-frame or sub-frame navigations). Default TT_EXPLICIT. Only available in the browser process.

Threading Constraint: Threading constraint not specified in source.

uint64_t GetIdentifier()

Return Value: Globally unique identifier, or 0 if not specified. Used by CefResourceRequestHandler implementations in the browser process to track one request across multiple callbacks.

Threading Constraint: Threading constraint not specified in source.

Usage Example

cpp
// BUILD a request for CefURLRequest:
CefRefPtr<CefRequest> req = CefRequest::Create();
req->SetURL("https://api.example.com/v1/data");
req->SetMethod("POST");
CefRefPtr<CefPostData> post = CefPostData::Create();
CefRefPtr<CefPostDataElement> el = CefPostDataElement::Create();
el->SetToBytes(payload.size(), payload.data());
post->AddElement(el);
req->SetPostData(post);
req->SetHeaderByName("Content-Type", "application/json", true);
req->SetFlags(UR_FLAG_NO_CACHE | UR_FLAG_NO_COOKIES);

CefRefPtr<CefURLRequest> urlRequest =
    CefURLRequest::Create(req, urlClient, request_context);

CefPostData

Source File: include/cef_request.h

Process / Thread Context: Any thread (per class comment).

Purpose: USER-CALLED library interface representing the post body of a CefRequest as an ordered collection of CefPostDataElement objects.

Methods

static CefRefPtr<CefPostData> Create()

Return Value: New empty CefPostData.

Threading Constraint: Threading constraint not specified in source.

bool IsReadOnly()

Return Value: True if read-only.

Threading Constraint: Threading constraint not specified in source.

bool HasExcludedElements()

Return Value: True if underlying POST data includes elements not represented by this object (e.g., multi-part file uploads). Modifying such an object may cause the request to fail.

Threading Constraint: Threading constraint not specified in source.

size_t GetElementCount()

Return Value: Number of elements.

Threading Constraint: Threading constraint not specified in source.

void GetElements(ElementVector& elements)

Return Value: Retrieves all elements (count via GetElementCount).

Threading Constraint: Threading constraint not specified in source.

bool RemoveElement(CefRefPtr<CefPostDataElement> element)

Return Value: Removes the specified element. True on success.

Threading Constraint: Threading constraint not specified in source.

bool AddElement(CefRefPtr<CefPostDataElement> element)

Return Value: Appends the specified element. True on success.

Threading Constraint: Threading constraint not specified in source.

void RemoveElements()

Return Value: Removes all elements.

Threading Constraint: Threading constraint not specified in source.

Usage Example

cpp
CefRefPtr<CefPostData> post = CefPostData::Create();
CefRefPtr<CefPostDataElement> bytes_el = CefPostDataElement::Create();
bytes_el->SetToBytes(sizeof(kPayload), kPayload);
post->AddElement(bytes_el);

CefRefPtr<CefPostDataElement> file_el = CefPostDataElement::Create();
file_el->SetToFile("/tmp/upload.bin");
post->AddElement(file_el);

request->SetPostData(post);

CefPostDataElement

Source File: include/cef_request.h

Process / Thread Context: Any thread (per class comment).

Purpose: USER-CALLED library interface representing one element of post data — either bytes or a file reference.

Methods

static CefRefPtr<CefPostDataElement> Create()

Return Value: New empty element.

Threading Constraint: Threading constraint not specified in source.

bool IsReadOnly()

Return Value: True if read-only.

Threading Constraint: Threading constraint not specified in source.

void SetToEmpty()

Return Value: Clears the element.

Threading Constraint: Threading constraint not specified in source.

void SetToFile(const CefString& fileName)

Return Value: Element becomes a file reference.

Threading Constraint: Threading constraint not specified in source.

void SetToBytes(size_t size, const void* bytes)

Return Value: Element becomes a byte buffer; the bytes are copied.

Threading Constraint: Threading constraint not specified in source.

Type GetType()

Return Value: Returns element type. Default PDE_TYPE_EMPTY.

Threading Constraint: Threading constraint not specified in source.

CefString GetFile()

Return Value: Returns the file name (only valid when type is PDE_TYPE_FILE).

Threading Constraint: Threading constraint not specified in source.

size_t GetBytesCount()

Return Value: Returns number of bytes (only valid when type is PDE_TYPE_BYTES).

Threading Constraint: Threading constraint not specified in source.

size_t GetBytes(size_t size, void* bytes)

Return Value: Copies up to size bytes into bytes and returns the number actually read.

Threading Constraint: Threading constraint not specified in source.

Usage Example

cpp
CefRefPtr<CefPostDataElement> el = CefPostDataElement::Create();
const char kJson[] = "{\"x\":1}";
el->SetToBytes(sizeof(kJson) - 1, kJson);  // bytes are copied
assert(el->GetType() == PDE_TYPE_BYTES);
std::vector<char> buf(el->GetBytesCount());
el->GetBytes(buf.size(), buf.data());  // read back

See Also

Derived from the CEF C++ headers — © Marshall A. Greenblatt, Google Inc. & contributors (BSD-style license). Not an official CEF project.