Forum Discussion

squirreltown's avatar
squirreltown
Roku Guru
12 years ago

Http headers

I am getting a http 400 error for what appears to be a valid request to Twitter. In the docs for URLtransfer is the following:
"Certain well known headers such as User-Agent, Content-Length, etc. will automatically be sent. The application may override these well known values if needed (e.g. some servers may require a specific user agent string)."


What exactly are the headers that are automatically sent?

thanks

6 Replies

  • The best way to verify exactly what headers you're sending is to send the request to a header echo server. There are a variety of such servers around; they will respond to a request with a copy of the headers that were sent to it. So change your script to temporarily send your query to that server instead of to the twitter server, and read the response to see what headers were sent. Here's a simple one that I wrote; feel free to use it: http://greenwoodsoftware.com/cgi-bin/echo-headers

    --Mark
  • OK not able to get any response in the debugger other than what i had before. Is this the right way to listen?
    oa = Oauth()
    'http = NewHttp("https://api.twitter.com/1.1/statuses/update.json","" ,"POST")
    http = NewHttp("http://greenwoodsoftware.com/cgi-bin/echo-headers","" ,"POST")
    oa.sign(http,true)
    http.PostFromStringWithTimeout( "", 10 )

    while(true)
    msg = wait(100,m.port)
    if type(msg) = "roUrlEvent"
    str = msg.GetString()
    print str
    exit while
    end if
    end while

    Alos where do I need to listen? Here? the tweet function is being called from a button on a canvas. Do i need to add a while loop in the NewHttp function? Also, my channel already has an m.port, but it seems like NewHttp is creating one too. Does the Roku allow 2 m.ports or does it know they are the same?
    thanks
  • PostFromStringWithTimeout() has it's own event loop and returns the response as a string. You don't need a second event loop. Yours is probably never getting a roUrlEvent because it's being caught by the one in PostFromStringWithTimeout().
  • Ok thanks Chris, You answered a question for me last week where you pointed out that the PostFromStringWithTimeout I was using was not the SDK one, but a different one in url.brs. As you see below there doesnt appear to be a loop here? (or in http_prep)
    Function http_post_from_string_with_timeout(val As String, seconds as Integer) as String
    m.Prep("POST")
    if (m.Http.AsyncPostFromString(val)) then m.Wait(seconds)
    return m.response
    End Function

    So that takes me back to the problem of not getting the headers back from that URL Mark supplied, which works fine in a browser. If i just change the URL from twitter to Mark's I only get the Http code response which is 400 from twitter and from Mark is 200. Where should i listen to get the whole thing with headers? Frankly I am not overly interested in the headers but I know the people on the Twitter developer board will be which is where i need to go with my bad request problem.

    Thanks

    EDIT:

    I think i found it, i needed to listen in -- Function http_wait_with_timeout(seconds As Integer) As Boolean. I got some headers back this time but not what i was expecting
    ACCEPT-ENCODING: deflate, gzip
    ACCEPT: */*
    USER-AGENT: Roku/DVP-5.1 (025.01E01195A)
    HOST: greenwoodsoftware.com

    At least moves the ball forward a little. Thanks again Chris.
  • One more thing, I am now trying to add headers. The function below has this"
    urlobj.AddHeader("Expect","")
    but its not showing in the echo headers I'm getting. Is the function not sending it or is the echo not returning it because it has no value? I tried adding a couple others but they dont show either.
    ACCEPT-ENCODING: deflate, gzip
    ACCEPT: */*
    USER-AGENT: Roku/DVP-5.1 (025.01E01195A)
    HOST: greenwoodsoftware.com


    Function http_prep(method="" As String)
    ' this callback allows just-before-send
    ' mods to the request, e.g. timestamp
    if isfunc(m.callbackPrep) then m.callbackPrep()
    m.status = 0
    m.response = ""
    urlobj = CreateObject("roUrlTransfer")
    if type(m.port)<>"roMessagePort" then m.port = CreateObject("roMessagePort")
    urlobj.SetPort(m.port)
    urlobj.SetCertificatesFile("common:/certs/ca-bundle.crt")
    urlobj.EnableEncodings(true)
    urlobj.AddHeader("Expect","")

    'urlobj.AddHeader("Content-Type","application/x-www-form-urlencoded") ''''
    'urlobj.AddHeader("Content-Length", "0") '''

    url = m.GetUrl()
    urlobj.SetUrl(url)
    if m.method<>"" and m.method<>method then urlobj.SetRequest(m.method)
    HttpActive().replace(m,urlobj)
    m.timer.mark()
    End Function