DEV Community

Discussion on: HTTP request with ES6 tagged templates

Collapse
 
kenbellows profile image
Ken Bellows • Edited

Huge fan of the idea! I'd love to see it accept something like the usual HTTP request format, with headers on the line below the URL, then a blank line, then a request body if there is one. I'd also love to see support for converting an object into query parameters, maybe even an object for additional headers, etc.

This request:

POST https://example.com/api/createMsg?apiKey=ABC123
Content-Type: application/json
Accept: application/json

{
    "title": "Test Message",
    "body": "This is a test of the messaging system."
}

could be made like this:

post`https://example.com/api.createMsg?${{apiKey: config.apiKey}}
Content-Type: application/json
Accept: application/json

${{
  title: 'Test Message',
  body: 'This is a test of the messaging system.'
}}
`
Collapse
 
vaheqelyan profile image
Vahe • Edited

Wow.it will look really good.maybe I should rethink and rewrite?

Collapse
 
vaheqelyan profile image
Vahe • Edited

But what about this

post`https://example.com/api.createMsg?${{apiKey: config.apiKey}} --content-json --accept-json`

Perhaps we can include both examples?

Collapse
 
kenbellows profile image
Ken Bellows

All depends on how complex you want your parser to be, I guess. Personally I'm not sold on a shell-command-like format, especially considering that it would be kind of annoying to build that string in JS I think. Would you specify just certain header options that can be specified with this shorthand?

I personally like the Header-Name: header-value syntax because it's very easy to parse into an Object, and its very easy to serialize an Object into that format. But it's all subjective; it's just a prototype, right? Have fun with it, try it out, use it for a project, see what you like and dislike

Thread Thread
 
vaheqelyan profile image
Vahe

I agree with you