Custom Request Headers And Rest.js

253 views
Skip to first unread message

Paul Tiseo

unread,
Dec 10, 2014, 9:59:34 AM12/10/14
to cuj...@googlegroups.com
In https://github.com/cujojs/rest/blob/master/docs/interfaces.md#interface-request, how does one actually specify a set of headers for a request, such as Content-Type or any custom heder? I realize I pass in a string, but how must it be formatted to be parsed properly?

var reqParams = {
        method
: 'GET',
        headers
: '???'
        path
: 'https://www.domain.com/rest/api/2/search'
     
}


Paul Tiseo

unread,
Dec 10, 2014, 10:40:26 AM12/10/14
to cuj...@googlegroups.com
nvm. it's just:


var reqParams = {
        method: 'GET',
        headers: {
          'Content-Type': 'application/json'
          'Some-Other-Header': 'turkey'
        },
        path: 'https://projects.commercialservices.com/rest/api/2/search?jql=%22Freshservice%20Tickets%22%20~%20%22%23' + ticketId + '%20%22'

Scott Andrews

unread,
Dec 10, 2014, 11:08:46 AM12/10/14
to cuj...@googlegroups.com
Hi Paul,

That is the correct way to specify HTTP headers. There are a couple of other things worth mentioning from your example:
- GET is the default method, so you don't need to specify it, unless you overrode the default in an interceptor.
- the query string should be defined by the params object. It will automatically URL encode the values for you.

So your request could look like:
var reqParams = {

    headers: {
        'Content-Type': 'application/json'
        'Some-Other-Header': 'turkey'
    },
    path: 'https://projects.commercialservices.com/rest/api/2/search',
    params: {
        jql: '"Freshservice Tickets" ~ "#' + ticketId + ' "'
    }
}

I find this easier to read, plus, if ticketId includes any URL unsafe characters, they will be encoded correctly.

-Scott

PS. It's uncommon to provide a Content-Type for a GET request since there is no entity. Did you mean Accept instead? The MIME Interceptor is your friend.

--
You received this message because you are subscribed to the Google Groups "cujojs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cujojs+un...@googlegroups.com.
To post to this group, send email to cuj...@googlegroups.com.
Visit this group at http://groups.google.com/group/cujojs.
To view this discussion on the web visit https://groups.google.com/d/msgid/cujojs/60552e03-8245-4ac4-b1de-ef8ac8356e72%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Paul Tiseo

unread,
Dec 10, 2014, 12:13:43 PM12/10/14
to cuj...@googlegroups.com
Appreciate the feedback! Thanks.

Re PS: I didn't write the server fielding the GET, unfortunately. :) With the default content-type of 'text/plain', it chokes with a 415 server error.

Scott Andrews

unread,
Dec 10, 2014, 1:19:04 PM12/10/14
to cuj...@googlegroups.com
It's impressive that is knows what a 415 is, but doesn't know that GETs don't have a Content-Type.

Reply all
Reply to author
Forward
0 new messages