0

We use application-only authentication and get back an access token. We then make a GET request to a specific user's timeline by doing the following:

_bpos = snprintf(_buffer, sizeof(_buffer)-1, "GET /1.1/statuses/user_timeline.json?user_id=dog_rates&count=1 HTTP/1.1\n" //This is the HTTP request that gets no reply at all, even though it does send
                                                 "Host: api.twitter.com\n"
                                                 "Authorization: Bearer %s",authToken);
    

But somehow this works:

_bpos = snprintf(_buffer, sizeof(_buffer) - 1, "POST /oauth2/token HTTP/1.1\n"  //I used this to test to make sure the following read/write works, it did for me
                                                      "Host: api.twitter.com\n"
                                                      "Authorization: Basic 12312312312345678901234567890\n"
                                                      "Content-Type: application/x-www-form-urlencoded;charset=UTF-8\n"
                                                      "Content-Length: 29\n\n"
                                                      "grant_type=client_credentials");

So it seems that only request of type GET are troublesome. I Any help would be appreciated!

Code

_bpos = snprintf(_buffer, sizeof(_buffer)-1, "GET /1.1/statuses/user_timeline.json?user_id=dog_rates&count=1 HTTP/1.1\n" //This is the HTTP request that gets no reply at all, even though it does send
                                                 "Host: api.twitter.com\n"
                                                 "Authorization: Bearer %s",authToken);

    mbedtls_printf("Buffer content:\n%s\n",_buffer);   //prints the content of the buffer to ensure the HTTP request is right

    /* Sending REST API GET request */
    ret = mbedtls_ssl_write(&_ssl, (const unsigned char *) _buffer, _bpos); //writes the data to the socket
    mbedtls_printf("how far u gonna ret: %d\r\n",ret); //prints the return value of the write, positive is number of bytes written (should be equal to _bpos), negative is a failure
    if (ret < 0) //Checks to see if write failed
    {
        if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) //If write failed and didnt give back a read/write error, the TLS connection is broken, and the program prints an error
        {
            print_mbedtls_error("mbedtls_ssl_write", ret);
            onError(_tcpsocket, -1 );
        }
        return -1;
    }


     /* Read data out of the socket */
    usedspace = 0;
    bufptr = (unsigned char *) _buffer;//this and the above line are used to concatenate multiple reads into one buffer, however second read is commented out for now.

    ret = mbedtls_ssl_read(&_ssl, bufptr, static_cast<size_t>(RECV_BUFFER_SIZE-usedspace)); //read from socket into buffer
    mbedtls_printf("how far u gonna ret: %d\r\n",ret);//prints return value of read, same deal as write return value
    if(ret < 0)//same check that write does
    {
        if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
        {
            print_mbedtls_error("mbedtls_ssl_read", ret);
            onError(_tcpsocket, -1 );
        }
        delete[] buf;
        return -1;
    }
    mbedtls_printf("read finished\r\n");//placeholder text to see if you get this far

    usedspace = usedspace+ret;

    bufptr = bufptr + ret;
Community
  • 1
  • 1
14wml
  • 4,048
  • 11
  • 49
  • 97

1 Answers1

0

Your HTTP requests are not valid. You are missing a newline after your last header in the GET request, and you're separating header lines with \n, and not with \r\n (also see this).

I'd suggest you use a library like mbed-http to build your requests for you.

Community
  • 1
  • 1
Jan Jongboom
  • 26,598
  • 9
  • 83
  • 120