简体   繁体   English

无法将GET请求发送到Twitter API,但可以发送POST请求吗?

[英]Unable to send GET request to Twitter API, but can send POST request?

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: 然后,我们通过执行以下操作向特定用户的时间轴发出GET请求:

_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. 因此,似乎只有GET类型的请求很麻烦。 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;

Your HTTP requests are not valid. 您的HTTP请求无效。 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 ). 您在GET请求中的最后一个标头之后缺少换行符,并且用\\n分隔标头行,而不用\\r\\n分隔标头行(另请参阅此内容 )。

I'd suggest you use a library like mbed-http to build your requests for you. 我建议您使用mbed-http之类的库来为您建立请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM