Streaming parameters


#1

Should I put the parameters (ex. track=twitter) in both the body and in the URL?

With Streaming API 1.0 I used this URL
https://stream.twitter.com/1/statuses/filter.json
and put the parameters in the body.

When I do the same with Streaming API 1.1 URL
https://stream.twitter.com/1.1/statuses/filter.json
and put the parameters in the body, I get a 401 Unauthorized.
However, when I also include the parameters in the URL like this
https://stream.twitter.com/1.1/statuses/filter.json?track=twitter
it works fine.


#2

You should put them in one or the other. If you’re sending a signifigant amount of data (criteria), you may want to use a POST explicitly.

Are you using OAuth when connecting to this API? If so, it’s possible you’re running into the system being a little stricter in 1.1. If you’re still having trouble, it’d be helpful if you could share your signature base string along with the POST body you’re sending.

If you’re sending multiple terms, often comma mis-encoding is to blame.


#3

I am using OAuth and POST. I thought that with POST the parameters should go in the body and not in the url, and visa versa with GET.

Using the following scenario I get a valid stream back from twitter (but, I am suspicious because I am putting the track parameters in BOTH the body and the url, even though my understanding is the parameters belong only in the body for a POST).

URL: https://stream.twitter.com/1.1/statuses/filter.json&track=zzz

BODY: track=zzz

OAUTH HEADER: OAuth oauth_consumer_key=“uOIeuhbP7I3bezsxJwIfNg”,oauth_nonce=“NjM0ODM2NTQwMjk1MzEyNTAw”,oauth_signature=“BTNeOmknDMTco8FHitWu0cTaHKk%3D”,oauth_signature_method=“HMAC-SHA1”,oauth_timestamp=“1348071630”,oauth_token=“295797426-7ePFwQbWQ0wLQght65DJqkXlivkVKYMCMQPtFd3v”,oauth_version=“1.0”

So, the above works, though I don’t think it should because I have track=zzz embedded in the url. Now, the scenario below does not work (401), though I expected to work. The only difference is I don’t embed track=zzz in the url.

URL: https://stream.twitter.com/1.1/statuses/filter.json

BODY: track=zzz

OAUTH HEADER: OAuth oauth_consumer_key=“uOIeuhbP7I3bezsxJwIfNg”,oauth_nonce=“NjM0ODM2NTQzNjc0ODQzNzUw”,oauth_signature=“BgNG85eMtOaXcrPDLqy%2BqEp5k7I%3D”,oauth_signature_method=“HMAC-SHA1”,oauth_timestamp=“1348071967”,oauth_token=“295797426-7ePFwQbWQ0wLQght65DJqkXlivkVKYMCMQPtFd3v”,oauth_version=“1.0”


#4

I posted a response about an hour ago. Still don’t see. I wonder if this post will appear…


#5

I am using POST and OAuth. (I think I have no choice.) The post body is track=zzz. The OAuth header is

OAuth oauth_consumer_key=“uOIeuhbP7I3bezsxJwIfNg”,oauth_nonce=“NjM0ODM2NjAwOTMzNzUwMDAw”,oauth_signature=“LlJUQOHRg7Q%2F8XIHkQFE50tX5PA%3D”,oauth_signature_method=“HMAC-SHA1”,oauth_timestamp=“1348077693”,oauth_token=“295797426-7ePFwQbWQ0wLQght65DJqkXlivkVKYMCMQPtFd3v”,oauth_version=“1.0”

So, restating the problem. The above stuff works only when I append ?track=zzz to the url (in addition to embedding track=zzz inside the post body). Alternatively, I get a 401 error if I only embed track=zzz inside the post body.

I am puzzled because I thought POST required the parameters to be inside the post body and NOT appended to the url, and visa versa for GET.


#6

With GET requests, you can put parameters only in the querystring.

With POST requests, you can put parameters in either the querystring or the POST body, but you generally shouldn’t repeat the same parameters in both locations.

There are natural limits to how long a URL can get, so if you put a large amount of values in the querystring, you can run into some minor issues. This is why we recommend placed in the POST body in this case. There are also some minor encoding differences between a POST body and a querystring.

It sounds like your OAuth implementation may not be selecting the right strategies when working with values in these contexts.


#7

I didn’t realize it was valid for POST to embed parameters in either the body or the url. I’ll dig further. Thanks!


#8

You can use this command on linux to see stream on real time, without using the Oauth protocol, you use just a basic authentification, this method it s just for test because it donsnt use the SSL,

curl --user YouruserName:YourPassword "https://stream.twitter.com/1/statuses/filter.json?track=obama

or you can use this code on java with Oauth protocol and you need some libraries like (oauth.signpost --> for oauth ) :

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader;

import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TwitterSpaceTest {

final static Logger log = LoggerFactory.getLogger(TwitterSpaceTest.class);
private static final String PROXY_HOST = ".....";	
private static final int PROXY_PORT = ....;	

public static String readFrom() throws IOException {
	String url = "https://stream.twitter.com/1/statuses/filter.json?track=obama&track=romney";
	
			
	DefaultHttpClient client = new DefaultHttpClient();	
	
	HttpHost proxy = new HttpHost(PROXY_HOST, PROXY_PORT, "http");
	client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
	
	HttpPost post = new HttpPost(url);
	
	post.setHeader("Accept-Encoding", "deflate, gzip"); //To use a GZIP Stream 
    
    //--------  Sign a Post with oauth using oauth.signpost --------
    String consumerKey = "";
    String consumerSecret = "";	    
    CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
    
    String token = " ..";
    String tokenSecret = "....";
    consumer.setTokenWithSecret(token, tokenSecret);
    //--------------	    
    try {
		consumer.sign(post);
	} catch (OAuthMessageSignerException e) {
		log.info("ERROR AT OAUTH, OAuthMessageSignerException ,ERROR TYPE ",e,e);			
	} catch (OAuthExpectationFailedException e) {
		log.info("ERROR AT OAUTH,  OAuthExpectationFailedException   ,ERROR TYPE ",e,e);			
	} catch (OAuthCommunicationException e) {
		log.info("ERROR AT OAUTH,  OAuthCommunicationException   ,ERROR TYPE ",e,e);			
	}	    
    //--------------------------------------------------------------------------
    
    HttpResponse response = client.execute(post);
    log.info("--<<<" + response.getStatusLine());
    HttpEntity entity = response.getEntity();
    if (entity == null) throw new IOException("No entity");
    
    BufferedReader br = new BufferedReader(new InputStreamReader(
        entity.getContent(), "UTF-8"));
    while (true) {
       String line = br.readLine();
       if (line != null && !line.isEmpty()) {
       log.info("--> " + line);	
       return line;
       
    	//log.info("*-*-*-*-*-*-*-*-*-*-*--*-*---*-*-*-*-*-*-");
       }
    }
    
    //------
    Reader reader = new BufferedReader(new InputStreamReader(new StreamingGZIPInputStream(http.getInputStream());
    //------
    
    
    
}



public static void main(String[] args) {		
	try {
		log.info("l*l*l*l*l*l*l*l*l*l*l*l*l*l*l");
		//readFrom();
		log.info("MON RETOUR       :"+readFrom());
	} catch (IOException e) {
		log.info("",e);
	}
}

}

Good luck !!!


#9

hi i typed on cmd : curl https://stream.twitter.com/1.1/statuses/filter.json
or curl https://stream.twitter.com/1.1/statuses/filter.json?track=twitter
I got a 401 Unauthorized.
it’s been one week, haven’t worked!


#10

Make sure that you’re passing authentication. Beginning May 7th, this method will require OAuth authentication to function.


#11

i have about the same problem.
if i ad &language=en to the post body( delimited=length&track=Aegon&language=en), i get an 401 error. When i use “POST /1.1/statuses/filter.json?language=en” i get a 401 error as well.
What am i doing wrong? Is it allowed to use the language parameter?
The twitter docs seems to be unclear about this. Because on https://dev.twitter.com/docs/streaming-apis/parameters#language, language is one of the listed parameters, but on https://dev.twitter.com/docs/api/1.1/post/statuses/filter, language is Not listed as parameter


#12

Hi,

I’m trying to figure out how the parameters are encoded in the body when using POST on statuses/filter.

I was using GET before and it worked fine, but was getting rejected for a too long url.

While using POST I always get “No filter parameters found. Expect at least one parameter: follow track locations” so I guess encoding is not right.
-> is the payload type application/JSON ? or raw text ? or else?
-> if it is text, how should I pass the parameters? Write a line in body as if I was sending a query string ?

Any help appreciated !


#13

@h_oll,

Can you please provide an example of the request(s) that you are working with?