bruno icon indicating copy to clipboard operation
bruno copied to clipboard

[Feature Request] Export Request As Code Snippet

Open jonmchan opened this issue 2 years ago • 8 comments

Postman has a really handy feature where you can export a request as a code snippet. The code snippet supports curl, C, C#, Go, Java and a lot of other languages. It makes it really easy to prototype something in postman and then have a starting point for implementation. It would be great if we can do this in Bruno as well.

jonmchan avatar Nov 23 '23 00:11 jonmchan

Export Request As Code Snippet feature is already available in Bruno. image Hover over the request and click on 3 dots displayed against the request. After that click on Generate Code option.

cyrilgeorge153 avatar Nov 23 '23 02:11 cyrilgeorge153

Thanks for pointing that out! That's perfect!

jonmchan avatar Nov 23 '23 03:11 jonmchan

I might be blind, but I can't see that option on v1.2.0, can only see the options: "Rename", "Clone", "Delete". Maybe it's because it's a graphql query?

KarmaCop213 avatar Nov 28 '23 12:11 KarmaCop213

@KarmaCop213 Generate Code option not available for GraphQL request type.

cyrilgeorge153 avatar Nov 28 '23 13:11 cyrilgeorge153

Hello,

It would be great indeed to support export of grapqhl requests :)

I've started digging into the code, but I don't have more time now to continue. So I write down below what I've found so it could help future me or someone else to go one step further

The code in charge of enabling export into gql is [here] (https://github.com/usebruno/bruno/blob/5ca7f6b7ad221a62f3843788a6c23d9a353cc16d/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/index.js#L330)

But graphql code export is not working due to an error on this line: https://github.com/usebruno/bruno/blob/5ca7f6b7ad221a62f3843788a6c23d9a353cc16d/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/CodeView/index.js#L20

Here's a basic sample from buildHarRequest on a gql query

const harRequest = {
  method: 'POST',
  url: 'http://localhost:3001/graphql',
  httpVersion: 'HTTP/1.1',
  cookies: [],
  headers: [],
  queryString: [],
  postData: {
    mimeType: '',
    text: {
      query: 'query liveness {\n\tliveness\t\n}'
    }
  },
  headersSize: 0,
  bodySize: 0
};

But when calling new HTTPSnippet(harRequest) it throws the following error

[Error [HARError]: validation failed] {
  errors: [
    {
      keyword: 'type',
      dataPath: '.postData.text',
      schemaPath: 'postData.json#/properties/text/type',
      params: { type: 'string' },
      message: 'should be string'
    }
  ]
}

To understand the difference, I looked at harRequest with the same request written directly in 'HTTP format' instead of 'Grapqhl Format'

const sameRequestButWithoutGQL = {
  method: 'POST',
  url: 'http://localhost:3001/graphql',
  httpVersion: 'HTTP/1.1',
  cookies: [],
  headers: [],
  queryString: [],
  postData: {
    mimeType: 'application/json',
    text: '{\n  "query": \'query liveness {\\n\\tliveness\\t\\n}\'\n}'
  },
  headersSize: 0,
  bodySize: 0
};

So the main difference is in mimeType and in the text that should follow the GQL Spec, in this case I can generate HTTP Snippet.

To continue the fix, one should handle gql request differently based on item.type === 'graphql-request' here https://github.com/usebruno/bruno/blob/5ca7f6b7ad221a62f3843788a6c23d9a353cc16d/packages/bruno-app/src/utils/codegenerator/har.js#L34

Here's a file to continue working on to support gql code export

const harRequest = {
  method: 'POST',
  url: 'http://localhost:3001/graphql',
  httpVersion: 'HTTP/1.1',
  cookies: [],
  headers: [],
  queryString: [],
  postData: {
    mimeType: '',
    text: {
      query: 'query liveness {\n\tliveness\t\n}'
    }
  },
  headersSize: 0,
  bodySize: 0
};

const sameRequestButWithoutGQL = {
  method: 'POST',
  url: 'http://localhost:3001/graphql',
  httpVersion: 'HTTP/1.1',
  cookies: [],
  headers: [],
  queryString: [],
  postData: {
    mimeType: 'application/json',
    text: '{\n  "query": \'query liveness {\\n\\tliveness\\t\\n}\'\n}'
  },
  headersSize: 0,
  bodySize: 0
};

const { HTTPSnippet } = require('httpsnippet');
const htttSnippet = new HTTPSnippet(sameRequestButWithoutGQL);
console.log(htttSnippet.convert('shell', 'curl'));

ajubin avatar Dec 27 '23 12:12 ajubin

Although Bruno already has the Export Request As Code Snippet feature, +1 to support C#

NightWuYo avatar Jan 28 '24 08:01 NightWuYo

@KarmaCop213 Generate Code option not available for GraphQL request type.

Hi. Is this a feature planed?

Although it would be nice to be able to generate code Graphql request, instead of removing "Generate code" menu in dots menu, it could be disabled. That would tell user generate code is not yet available for this request.

tophsic avatar Mar 07 '24 05:03 tophsic

Also, the generated code doesn't take into account the pre-request script. In Postman you can copy the actual request being sent after the request has been modified by the script.

piekill avatar Mar 14 '24 11:03 piekill

as my organisation is migrating to graphql completely, being able to export graphql requests to curl would be an immensely appreciated feature, even without the capability to take into account pre-request scripts

i see there's a pull request already, would it be possible to consider merging it in?

https://github.com/usebruno/bruno/pull/1288

tobby-s avatar Apr 24 '24 05:04 tobby-s

Also, the generated code doesn't take into account the pre-request script. In Postman you can copy the actual request being sent after the request has been modified by the script.

I completely agree, and I'd like to add that the generated code does not include an authentication header or replace variables. I believe that there should be at least an option for that.

rusak47 avatar May 28 '24 16:05 rusak47