Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Export Request As Code Snippet #1031

Open
jonmchan opened this issue Nov 23, 2023 · 10 comments
Open

[Feature Request] Export Request As Code Snippet #1031

jonmchan opened this issue Nov 23, 2023 · 10 comments

Comments

@jonmchan
Copy link

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.

@cyrilgeorge153
Copy link

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.

@jonmchan
Copy link
Author

Thanks for pointing that out! That's perfect!

@KarmaCop213
Copy link

KarmaCop213 commented Nov 28, 2023

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?

@cyrilgeorge153
Copy link

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

@helloanoop helloanoop reopened this Nov 28, 2023
@ajubin
Copy link
Contributor

ajubin commented Dec 27, 2023

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] (

)

But graphql code export is not working due to an error on this line:

snippet = new HTTPSnippet(buildHarRequest({ request: item.request, headers })).convert(target, client);

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

const createPostData = (body) => {

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'));

@NightWuYo
Copy link

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

@tophsic
Copy link

tophsic commented Mar 7, 2024

@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.

@piekill
Copy link

piekill commented Mar 14, 2024

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.

@tobby-s
Copy link

tobby-s commented Apr 24, 2024

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?

#1288

@diabolusss
Copy link

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.

Its-treason added a commit to Its-treason/bruno that referenced this issue Jul 2, 2024
- GraphQL is now supported: usebruno#1031
- PowerShell generator works: usebruno#1566
- Collection and Folder level headers are now included: usebruno#943
- Variables are fully interpolated: usebruno#417
- Pre-Request-Script is executed for generation: usebruno#1261
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants