bruno
bruno copied to clipboard
[Feature Request] Export Request As Code Snippet
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.
Export Request As Code Snippet feature is already available in Bruno.
Hover over the request and click on 3 dots displayed against the request. After that click on Generate Code option.
Thanks for pointing that out! That's perfect!
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 Generate Code option not available for GraphQL request type.
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'));
Although Bruno already has the Export Request As Code Snippet feature, +1 to support C#
@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.
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.
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
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.