tencent cloud

Feedback

Obtaining Client IP Address

Last updated: 2023-12-13 10:57:07
    Since the front end cannot directly obtain the client IP address, it's often necessary to obtain the client IP address through the server side or third-party services in multiple business scenarios. The client IP address is obtained in this example through client IP header EO-Client-IP activated in the rule engine, and assembled into data in the form of JSON to respond to the clients, which succeeds in obtaining client IP address by the use of edge function.
    addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request));
    });
    
    function handleRequest(request) {
    // Obtain the client IP through the EO-Client-IP header
    const ip = request.headers.get('EO-Client-IP') || '';
    // Respond with JSON data
    return new Response(JSON.stringify({ ip }), {
    headers: { 'content-type': 'application/json' },
    });
    }

    Sample Preview

    Firstly, activate the client IP switch of the domain name that needs to trigger the edge function and set the header name as EO-Client-IP in the Rule Engine configuration.
    
    Once the configuration is effective, enter a URL (such as https://example.com/ip) which matches the trigger rule of the edge function in the address bar of the browser on both the PC and mobile terminal to obtain the client's IP address:
    

    Related References

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support