This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC3220: How to use custom http header

Part Number: CC3220

Hi,

I have a question from my customer about custom http header.
He wants to specify API Key and original ID to http header.

According to “Network Services 2.30.00.10 Release Notes”, it says
“NS-128: Enable HTTPClient to send custom headers in HTTP requests”.
So it seems custom header can be used.
(SDK version is 2.30.00.05)
But it is not clear how to do that.

Customer tried using
“HTTPClient_setHeader” or “HTTPClient_setHeaderByName”, but no success.
Error 403 is returned from a server.

Thanks and regards,
KoT

  • Hi,

    What return value is the customer getting from HTTPClient_setHeader? Also, our httpget demo uses HTTPClient_setHeader. It might help to see how it's used.

    Jesu
  • Hi Jesu,

    Thanks for your quick reply.

    HTTPClient_setHeader returns 0.
    And HTTPClient_sendRequest returns 403: HTTP_SC_FORBIDDEN.
    (when using emulator with stepping execution, HTTPClient_sendRequest returns -3003: HTTPClient_ERESPONSEINVALID)
    Please see below customer code snippet.

    // use HTTPClient_setHeader
    #define HOSTNAME                "https://xxxxxxxxxx.com"
    #define GET_MACHINE_INFO        "/pets/1"
    #define API_SET_HEADER_AUTH     "x-api-key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"\
         "x-user-uuid: xxxxxxx"
    #define API_SET_HEADER_ACCE     "application/json"
    
    void* httpTask(void* pvParameters)
    {
    
        bool moreDataFlag = false;
        char data[HTTP_MIN_RECV];
        int16_t ret = 0;
        int16_t len = 0;
    
    
        HTTPClient_Handle httpClientHandle;
        int16_t statusCode;
    
        // https enabling
        HTTPClient_extSecParams httpClientSecParams;
    
        httpClientSecParams.privateKey = NULL;
        httpClientSecParams.clientCert = NULL;
        httpClientSecParams.rootCa = "test-certificate.pem.crt";
        httpClientHandle = HTTPClient_create(&statusCode,0);
    
        ret =HTTPClient_setHeader(httpClientHandle,
                                  HTTPClient_HFIELD_REQ_AUTHORIZATION ,
                                  API_SET_HEADER_AUTH,strlen(API_SET_HEADER_AUTH),
                                 HTTPClient_HFIELD_PERSISTENT);
        ret =HTTPClient_setHeader(httpClientHandle,
                                  HTTPClient_HFIELD_REQ_CONTENT_TYPE ,
                                  API_SET_HEADER_ACCE,strlen(API_SET_HEADER_ACCE),
                                 HTTPClient_HFIELD_PERSISTENT);
    
    
        strcpy(t_Url,HOSTNAME);
    
        ret = HTTPClient_connect(httpClientHandle,t_Url,&httpClientSecParams,0);
    
        strcpy(t_BodyMsg_s,GET_MACHINE_INFO);
        ret = HTTPClient_sendRequest(httpClientHandle,HTTP_METHOD_GET,t_BodyMsg_s,
                                   NULL,0,
                                   0);



    Customer also tried to use HTTPClient_setHeaderByName. Results are exactly the same.

    //  use HTTPClient_setHeaderByName
    
    #define HOSTNAME                "https://xxxxxxxxxx.com"
    #define GET_MACHINE_INFO        "/pets/1"
    #define API_SET_HEADER_AUTH     "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    #define API_SET_HEADER_UUID     "xxxxx"
    #define API_SET_HEADER_ACCE     "application/json"
    
    
    void* httpTask(void* pvParameters)
    {
    
        bool moreDataFlag = false;
        char data[HTTP_MIN_RECV];
        int16_t ret = 0;
        int16_t len = 0;
    
    
        HTTPClient_Handle httpClientHandle;
        int16_t statusCode;
    
        // https enabling
        HTTPClient_extSecParams httpClientSecParams;
    
        httpClientSecParams.privateKey = NULL;
        httpClientSecParams.clientCert = NULL;
        httpClientSecParams.rootCa = "test-certificate.pem.crt";
        httpClientHandle = HTTPClient_create(&statusCode,0);
    
        strcpy(t_Url,"x-api-key");
        ret =HTTPClient_setHeaderByName(httpClientHandle,
                                 HTTPClient_HFIELD_REQ_AUTHORIZATION ,
                                 t_Url,
                                 API_SET_HEADER_AUTH,strlen(API_SET_HEADER_AUTH),
                                 HTTPClient_HFIELD_PERSISTENT);
        strcpy(t_Url,"x-user-uuid");
        ret =HTTPClient_setHeaderByName(httpClientHandle,
                                 HTTPClient_HFIELD_REQ_AUTHORIZATION,
                                 t_Url,
                                 API_SET_HEADER_UUID,strlen(API_SET_HEADER_UUID),
                                 HTTPClient_HFIELD_PERSISTENT);
        strcpy(t_Url,"accept");
        ret =HTTPClient_setHeaderByName(httpClientHandle,
                                  HTTPClient_HFIELD_REQ_CONTENT_TYPE ,
                                  t_Url,
                                  API_SET_HEADER_ACCE,strlen(API_SET_HEADER_ACCE),
                                 HTTPClient_HFIELD_PERSISTENT);
    
    
        strcpy(t_Url,HOSTNAME);
        ret = HTTPClient_connect(httpClientHandle,t_Url,&httpClientSecParams,0);
    
        strcpy(t_BodyMsg_s,GET_MACHINE_INFO);
        ret =HTTPClient_sendRequest(httpClientHandle,HTTP_METHOD_GET,t_BodyMsg_s,
                                   NULL,0,
                                   0);



    Thanks and regards,
    KoT

  • Hi KoT,

    I don't see any issues with the code and we know the request is getting send because the server acknowledged it. You should double check your header values to make sure they're correct. Also check if the server has additionally requirements or limitation. What server are you trying to contact?

    Jesu
  • Hello

    I understood the cause of this problem.
         ret = HTTPClient_setHeaderByName (httpClientHandle,
                                  HTTPClient_HFIELD_REQ_AUTHORIZATION,
                                  t_Url,
                                  API_SET_HEADER_AUTH, strlen (API_SET_HEADER_AUTH),
                                  HTTPClient_HFIELD_PERSISTENT);

    Of

    strlen (API_SET_HEADER_AUTH)
    It changed to

    strlen (API_SET_HEADER_AUTH) +1.

    Of the document
    > HTTPClient_setHeaderByName function
    > Specify the header name "name" By the way
    > "Name of header. Must be NULL-terminated."
    I was aware of the description that, but I did not know that it was necessary to add NULL-terminated minutes to the length.
    By making it +1, it is possible to acquire HTTP Response "200"

    Thank you very much.